java处理文本文件中的乱码字符
逐行读取文本文件,使用正则表达式替换其中出现的乱码,并将清除掉乱码的数据保存到新文件中
package com.yonyou.jekey; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; /* * */ public class Replacelm { public static BufferedReader bufread; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String input = args[0]; String output = args[1]; File outputf = creatTxtFile(output); String read, write; String regex = "[^\\p{Graph}\\s]"; FileReader fileread; fileread = new FileReader(input); bufread = new BufferedReader(fileread); FileOutputStream fos = new FileOutputStream(outputf); PrintWriter pw = new PrintWriter(fos); while ((read = bufread.readLine()) != null) { write = read.replaceAll(regex, "")+"\r\n"; pw.write(write.toCharArray()); } pw.flush(); pw.close(); System.out.println("处理完成!"); System.out.println("请查看文件:"+outputf.getAbsolutePath()); } /** * 创建文本文件. * * @throws IOException * */ public static File creatTxtFile(String filename) throws IOException { File f = new File(filename); if (!f.exists()) { f.delete(); f.createNewFile(); } return f; } }