[Java] java处理文本文件中的乱码字符 →→→→→进入此内容的聊天室

来自 , 2020-10-24, 写在 Java, 查看 168 次.
URL http://www.code666.cn/view/a09e75c5
  1. package com.yonyou.jekey;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9.  
  10. /*
  11.  *
  12.  */
  13. public class Replacelm {
  14.         public static BufferedReader bufread;
  15.  
  16.         /**
  17.          * @param args
  18.          * @throws IOException
  19.          */
  20.         public static void main(String[] args) throws IOException {
  21.                 // TODO Auto-generated method stub
  22.                 String input = args[0];
  23.                 String output = args[1];
  24.  
  25.                 File outputf = creatTxtFile(output);
  26.  
  27.                 String read, write;
  28.                 String regex = "[^\\p{Graph}\\s]";
  29.                 FileReader fileread;
  30.                 fileread = new FileReader(input);
  31.                 bufread = new BufferedReader(fileread);
  32.  
  33.                 FileOutputStream fos = new FileOutputStream(outputf);
  34.                 PrintWriter pw = new PrintWriter(fos);
  35.  
  36.                 while ((read = bufread.readLine()) != null) {
  37.                         write = read.replaceAll(regex, "")+"\r\n";
  38.                         pw.write(write.toCharArray());
  39.  
  40.                 }
  41.                 pw.flush();
  42.                 pw.close();
  43.                 System.out.println("处理完成!");
  44.                 System.out.println("请查看文件:"+outputf.getAbsolutePath());
  45.         }
  46.  
  47.         /**
  48.          * 创建文本文件.
  49.          *
  50.          * @throws IOException
  51.          *
  52.          */
  53.         public static File creatTxtFile(String filename) throws IOException {
  54.                 File f = new File(filename);
  55.                 if (!f.exists()) {
  56.                         f.delete();
  57.                         f.createNewFile();
  58.                 }
  59.                 return f;
  60.         }
  61.  
  62. }
  63.  
  64. //java/5678

回复 "java处理文本文件中的乱码字符"

这儿你可以回复上面这条便签

captcha