zoukankan      html  css  js  c++  java
  • 自动化测试---文件内容的读写

    一、execl文件

    POI第三方工具,读写execl文件

    1、读取execl文件

    具体说明:http://blog.csdn.net/u011541946/article/details/74726045

        package lessons;  
          
        import java.io.File;  
        import java.io.FileInputStream;  
        import org.apache.poi.xssf.usermodel.XSSFSheet;  
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
          
        public class ReadExcel {  
          
            public static void main(String[] args) {  
                  
                try{  
                      
                    // 指定excel的路径  
                    File src = new File(".\Files\test-data.xlsx");  
                      
                    // 加载文件  
                    FileInputStream fis = new FileInputStream(src);  
                      
                    // 加载workbook  
                    @SuppressWarnings("resource")  
                    XSSFWorkbook wb=new XSSFWorkbook(fis);  
                      
                    //加载sheet,这里我们只有一个sheet,默认是sheet1  
                    XSSFSheet sh1= wb.getSheetAt(0);  
                      
                    // 开始读取第一行第一列的数据  
                    System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());  
                    // 读取第一行第二列内容  
                    System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());  
                    // 读取第二行第二列内容  
                    System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());  
                    // 读取第二行第二列内容  
                    System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());  
                      
                    // 获取实际总行数  
                    System.out.println(sh1.getPhysicalNumberOfRows());  
                      
                    // 获取实际总列数  
                    System.out.println(sh1.getPhysicalNumberOfRows());  
                      
                } catch (Exception e){  
                      
                    System.out.println(e.getMessage());  
                      
                }  
          
            }  
          
        }  
    // i 控制行  
                for(int i=0;i<sh1.getPhysicalNumberOfRows();i++){  
                    // j是控制列,我们上面数据是2列  
                    for(int j=0;j<2;j++){  
                          
                        System.out.println(sh1.getRow(i).getCell(j).getStringCellValue());  
                    }  
                }  

    2、写入excel

        package lessons;  
          
        import java.io.File;  
        import java.io.FileInputStream;  
        import java.io.FileOutputStream;  
          
        import org.apache.poi.xssf.usermodel.XSSFSheet;  
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
          
        public class ReadExcel {  
          
            public static void main(String[] args) {  
                  
                try{  
                      
                    // 指定excel的路径  
                    File src = new File(".\Files\test-data.xlsx");  
                      
                    // 加载文件  
                    FileInputStream fis = new FileInputStream(src);  
                      
                    // 加载workbook  
                    @SuppressWarnings("resource")  
                    XSSFWorkbook wb=new XSSFWorkbook(fis);  
                      
                    //加载sheet,这里我们只有一个sheet,默认是sheet1  
                    XSSFSheet sh1= wb.getSheetAt(0);  
                      
                    //写入excel数据  
                    sh1.getRow(0).createCell(2).setCellValue("Pass");  
                    sh1.getRow(1).createCell(2).setCellValue("Fail");  
                    sh1.getRow(2).createCell(2).setCellValue("N/A");  
                    sh1.getRow(3).createCell(2).setCellValue("Pass");  
                  
                    // 保存文件  
                    FileOutputStream fout=new FileOutputStream(new File(".\Files\test-data.xlsx"));  
                       
                    //覆盖写入内容   
                    wb.write(fout);  
                      
                    // 关闭文件  
                    fout.close();  
                      
                } catch (Exception e){  
                      
                    System.out.println(e.getMessage());  
                      
                }  
          
            }  
          
        }  

    二、.prperties读取

        package lessons;  
          
        import java.io.FileInputStream;  
        import java.io.IOException;  
        import java.io.InputStream;  
        import java.util.Properties;  
          
        public class ReadPropertiesFile {  
              
            public static String browser_Name;  
            public static String server_Url;  
              
            public static void main(String[] args) throws IOException {  
                  
                Properties p = new Properties();  
                InputStream ips = new FileInputStream(".\TestConfig\config.properties");  
                p.load(ips);  
                  
                browser_Name=p.getProperty("browserName");  
                server_Url = p.getProperty("serverUrl");  
                System.out.println(browser_Name);  
                System.out.println(server_Url);  
                ips.close();  
                      
            }  
          
        }  
  • 相关阅读:
    http协议的状态码——400,401,403,404,500,502,503,301,302等常见网页错误代码
    JS中的动态合集与静态合集
    对联
    诗词
    文言文
    Youth Is Not a Time of Life
    JS探秘——那些你理解存在偏差的问题
    JS中的加号+运算符详解
    支持HTTP2协议
    银行卡信息查询接口
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/7804982.html
Copyright © 2011-2022 走看看