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

xiaoxiao2025-07-26  36

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

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("文件读取错误!");         }

    }

转载请注明原文地址: https://www.6miu.com/read-5033757.html

最新回复(0)