java-获取.csv文件里的数据,并且获取文件夹下所含有对象的个数

来源:互联网 发布:mac迅雷2.6.7 编辑:程序博客网 时间:2024/06/01 11:21
<span style="white-space:pre"></span>//如果是浏览器运行,就不需要写WebContent/ 了String path = "WebContent/config";File file = new File(path);File[] tempList = file.listFiles();System.out.println("该文件夹目录下对象的个数:" + tempList.length);       <span style="white-space:pre"></span> /**         <span style="white-space:pre"></span>*  读取csv文件里的数据         <span style="white-space:pre"></span>*  因为要获取文件只是csv的文件,所以截取字符串,把文件类型获取出来,此处就是获取字符串的最后三个字符         <span style="white-space:pre"></span>*/for (int i = 0; i < tempList.length; i++) {if (tempList[i].isFile()) {System.out.println("文 件:" + tempList[i]);ArrayList<String[]> csvList = new ArrayList<String[]>(); // 用来保存数据String csvPath;csvPath = tempList[i].getPath();String type = csvPath.substring(csvPath.length() - 3, csvPath.length());System.out.println(type);if (type.equals("csv")) {CsvReader reader = new CsvReader(csvPath, ',', Charset.forName("SJIS")); // 一般用这个编码就可以了reader.readHeaders(); // 跳过表头while (reader.readRecord()) { // 逐行读入出表头的数据csvList.add(reader.getValues());}reader.close();for (int row = 0; row < csvList.size(); row++) {String cell = csvList.get(row)[0] + "," + csvList.get(row)[1]; // 取得第row行第0列的数据System.out.println(cell);}}} else if (tempList[i].isDirectory()) {System.out.println("文件夹:" + tempList[i]);}
         此截图就是文件所存放的路径
1 0