Java代碼一行一行讀取txt的內容


 
         
public static void main(String[] args) {
        // 文件夾路徑 
        String path = "E:\\eclipse work\\ImageUtil\\src\\scan.txt";
        List<String> scanListPath = readFile02(path);
    }



/**
     * 讀取一個文本 一行一行讀取
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static List<String> readFile02(String path) throws IOException {
        // 使用一個字符串集合來存儲文本中的路徑 ,也可用String []數組
        List<String> list = new ArrayList<String>();
        FileInputStream fis = new FileInputStream(path);
        // 防止路徑亂碼   如果utf-8 亂碼  改GBK     eclipse里創建的txt  用UTF-8,在電腦上自己創建的txt  用GBK
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) {
            // 如果 t x t文件里的路徑 不包含---字符串       這里是對里面的內容進行一個篩選
            if (line.lastIndexOf("---") < 0) {
                list.add(line);
            }
        }
        br.close();
        isr.close();
        fis.close();
        return list;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM