1.读取txt文件:
txt文件:
test1
test2
关键代码:
InputStreamReader inFile = new InputStreamReader(new FileInputStream(file));
BufferedReader reader = new BufferedReader(inFile); String data = null; while((data = reader.readLine())!=null){ System.out.println(data); }2.读取csv文件(读取一组数据,需引入JavaCsv.jar包):
csv文件:
name | age | sex | |
test | 123456@qq.com | 23 | W |
test1 | 123456@qq.com | 24 | N |
test2 | 123456@qq.com | 25 | W |
关键代码:
ArrayList<String[]> arrayList = new ArrayList<String[]>();
CsvReader reader = new CsvReader(file); reader.readHeaders();//跳过首行 while(reader.readRecord()){ arrayList.add(reader.getValues()); } //输入内容 for(int i=0;i<arrayList.size();i++){ System.out.println("name:"+arrayList.get(i)[0]); System.out.println("email:"+arrayList.get(i)[1]); System.out.println("age:"+arrayList.get(i)[2]); System.out.println("sex:"+arrayList.get(i)[3]); System.out.println("--------------------"); }3.xml文件(存储不规则数据):
xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<config> <browser>firefox</browser> <waitTime>20</waitTime> </config>关键代码:
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);