Java IO file encoding conversion implementation Code _java

Source: Internet
Author: User

Do not really understand the IO operation ... On the code, garbled is also a smattering of ... Today we have a requirement to encode a file and return the encoded string, such as the original GBK encoding, to the UTF-8

One of the Bytesencodingdetect classes is not posted. The main use of the inside of the Get file encoding format.

Just started to try directly in the source file to modify the encoding method, using Urlencoder and urldecoder for conversion, but delay. There is a Chinese odd number last word garbled

Baidu found a solution, have no fruit, had to use my idea is: first read the contents of the source file, stored in the stringbuffer inside, and then delete the source file, and then a new file, and then another in the form of encoding stored in.

To view the effect of encoding: note that you do not view the effect in Eclipse, Eclipse is viewed in an encoded form, so you can determine whether it is successful by looking at the HTML file in the browser side, viewing the specified encoding as the right key---encoding.

Copy Code code as follows:

Package com.test;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;

public class Transcoding {
Private Bytesencodingdetect encode = new Bytesencodingdetect ();
Public transcoding () {
}

/**
* Code Conversion
* @param tocharset the encoding to be converted
* @param path of the file to be converted
* @return
* @throws Exception
*/
public string encoding (string tocharset, String path) throws exception{
File Srcfile = new file (path);
int index = encode.detectencoding (srcfile);
String charset = Bytesencodingdetect.javaname[index];
Encoding is the same without transcoding
if (Charset.equalsignorecase (Tocharset)) {
Return "code, no conversion required";
}

InputStream in = new FileInputStream (path);

BufferedReader br = new BufferedReader (
New InputStreamReader (in, CharSet));

StringBuffer sb = new StringBuffer ();
String S1;
while ((S1=br.readline ())!=null) {
String s = Urlencoder.encode (S1, Tocharset);
Sb.append (s+ "\ r \ n");//Line + carriage return
}

Br.close ();
Srcfile.delete ()//delete original file
Re-write the file with the new encoding and return the value
FileName NewFile = new file (path);//Rebuild Original files
Newfile.createnewfile ();
OutputStream out = new FileOutputStream (newfile);
OutputStreamWriter writer = new OutputStreamWriter (out, tocharset);
BufferedWriter bw = new BufferedWriter (writer);
Bw.write (Urldecoder.decode (sb.tostring (), tocharset));
String result = Urldecoder.decode (Sb.tostring (), tocharset);
Bw.flush ()//Brush to File
Bw.close ();
return result;
}

}

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.