java-擷取.csv檔案裡的資料,並且擷取檔案夾下所含有對象的個數

來源:互聯網
上載者:User
<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]);}
         此截圖就是檔案所存放的路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.