java读取本地文本文件然后做简单处理之后再输出到本地另外一个文本中

针对一些很大的文本文件用工具打开编辑很慢的情况

public void readTxtFile(String filePath) {
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                FileWriter fw = new FileWriter("C:\\Users\\wangr\\Desktop\\深圳cntf数据\\test.csv",true);
                while ((lineTxt = br.readLine()) != null) {
                    System.out.println(lineTxt); 
                    /*
                     * 调用Writer对象中的write(string)方法,写入数据。 
                     * 其实数据写入到临时存储缓冲区中。
                     */
                    fw.write((lineTxt.replace("\"", ""))+"\r\n");//LINE_SEPARATOR是换行符 
                    
                }
                //进行刷新,将数据直接写到目的地中。    注意:flush可以用多次。
                fw.flush();
                
                //关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。        注意:close可以用多次。
                fw.close();
                br.close();
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误!");
        }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章