Java file encoding tool and java coding Tool

Source: Internet
Author: User

Java file encoding tool and java coding Tool

This article mainly introduces a tool class written in java to convert a file encoding to another encoding that does not change the file content:

You can use URLEncoding to re-encode and decode the source file.

     

    

1 public class ChangeFileEncoding {2 public static int fileCount = 0; 3 public static String sourceFileRoot = "Replace with the source file or directory to be converted "; // The root directory of the file to be converted 4 public static String sourceCharset = "GB2312"; // The source file encoding 5 public static String targetCharset = "utf8 "; // target File code 6 public static void main (String [] args) throws IOException {7 File fileDir = new File (sourceFileRoot); 8 convert (fileDir); 9 System. out. pri Ntln ("Total Dealed:" + fileCount + "Files"); 10} 11 12 public static void convert (File file) throws IOException {13 // if it is a file, the code is converted, and the write overwrites the original file 14 if (file. isFile () {15 // only process. code file ending with java 16 if (file. getPath (). indexOf (". java ") =-1) {17 return; 18} 19 InputStreamReader isr = new InputStreamReader (new FileInputStream (20 file), sourceCharset ); 21 BufferedReader br = new BufferedReader (isr); 22 StringBuffe R sb = new StringBuffer (); 23 String line = null; 24 while (line = br. readLine ())! = Null) {25 // note that the line break 26 line = URLEncoder is written. encode (line, "utf8"); 27 sb. append (line + "\ r \ n"); // The line break in windows is \ r \ n28} 29 br. close (); 30 isr. close (); 31 32 File targetFile = new File (file. getPath (); 33 OutputStreamWriter osw = new OutputStreamWriter (34 new FileOutputStream (targetFile), targetCharset); 35 BufferedWriter bw = new BufferedWriter (osw ); 36 // write 37 bw at a time in the form of a string. write (URLDecoder. decode (sb. toString (), "utf8"); 38 bw. close (); 39 osw. close (); 40 41 System. out. println ("Deal:" + file. getPath (); 42 fileCount ++; 43} else {44 // recursively. encode and convert objects ending with java 45 for (File subFile: file. listFiles () {46 convert (subFile); 47} 48} 49} 50 51}

 

 

    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.