批量将文字转换为encoding格式

按行读取文件中,转换后并保存文件。以下为代码:

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


public class Example {

 /**
  * @param args
  */
 
 public static String getEncoding(String str){
  
  String st=null;
  try {
   st=java.net.URLEncoder.encode(str,"UTF-8");
   //st=escape(encodeURIComponent(str));
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return st;
 }
 /*
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  System.out.println(Example.getEncoding("湄公河 遇袭"));
 }
 */
 public static void main(String[] args) {
  
  String fileout = "c:\\wan\\keywd.txt";
  String filein = "c:\\wan\\kw.txt";
     
  File fileo = new File(fileout);
  File filei = new File(filein);
  BufferedReader reader = null;
  FileWriter fw = null;
  //FileReader fr = new FileReader(fileo);


  try{
   //reader = new BufferedReader(new FileReader(fileo)); 
   FileReader fr = new FileReader(fileo);
   reader = new BufferedReader(fr); 
   String tempString = null; 
   String newString = null;
   fw=new FileWriter(filei,true);
   //一次读入一行,直到读入null为文件结束  
   while ((tempString = reader.readLine()) != null){ 
    newString = Example.getEncoding(tempString)+"\r\n";
    System.out.println(Example.getEncoding(tempString));
       //写入文件  
       fw.write(newString);
       fw.flush();
   } 
   reader.close();
   fw.close();  
  } catch (IOException e) { 
   e.printStackTrace(); 
    } finally { 
       if (reader != null)
       try { 
       reader.close(); 
       } catch (IOException e1) { 
         }
   }
  
 } 
}

你可能感兴趣的:(性能测试,encoding,string,null,file,import,class)