Summary and examples of three methods of java character transcoding
- 2020-06-15 09:04:54
- OfStack
java character transcoding: Three methods
Prerequisite for successful transcoding: no messy code after decoding
Transcoding process: file (gbk)--
>
Decoding -
>
- coding
>
File (utf - 8)
Note: Please leave a message if you have any questions
The following is a concrete example
Method 1: Java. lang. String
// Constructor for decoding :
String(byte[] bytes, int offset, int length, String charsetName)
String(byte[] bytes, String charsetName)
The method used for coding :
byte[] getBytes(String charsetName) // Encode using the specified character set
byte[] getBytes() // Use the system default character set for encoding
public void convertionString() throws UnsupportedEncodingException{
String s = " mountain ";
byte[] b = s.getBytes("gbk");// coding
String sa = new String(b, "gbk");// decoding : Which character set is used for decoding
System.out.println(sa);
b = sa.getBytes("utf-8");// coding
sa = new String(b, "utf-8");// decoding
System.err.println(sa);
}
Method 2: java. io. InputStreamReader/OutputStreamWriter: bridge transformation
package com.qingshan.io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* <pre>
* use java.io Bridge transformation : Transcode the file
* </pre>
* <hr Color="green" ></hr>
* 2012 Qingshan Group All rights reserved
* <hr Color="green" ></hr>
* @author thetopofqingshan
* @version 1.0.0
* @since JDK 1.5
* @date 2012-4-28
*/
public class CharsetConvertion {
private FileInputStream fis;// File input stream : Read the contents of the file
private InputStream is;
private InputStreamReader isr;
private OutputStream os;
private OutputStreamWriter osw;// write
private char[] ch = new char[1024];
public void convertionFile() throws IOException{
is = new FileInputStream("C:/ Project progress tracking .txt");// File to read
isr = new InputStreamReader(is, "gbk");// decoding
os = new FileOutputStream("C:/ Project progress tracking _utf-8.txt");// The output file
osw = new OutputStreamWriter(os, "utf-8");// Start coding
char[] c = new char[1024];// The buffer
int length = 0;
while(true){
length = isr.read(c);
if(length == -1){
break;
}
System.out.println(new String(c, 0, length));
osw.write(c, 0, length);
osw.flush();
}
}
public void convertionString() throws UnsupportedEncodingException{
String s = " Mountain group, ";
byte[] b = s.getBytes("gbk");// coding
String sa = new String(b, "gbk");// decoding : Which character set is used for decoding
System.out.println(sa);
b = sa.getBytes("utf-8");// coding
sa = new String(b, "utf-8");// decoding
System.err.println(sa);
}
/**
* <pre>
* Close all streams
* </pre>
*
*/
public void close(){
if(isr != null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(osw != null){
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* <pre>
* with io Read file contents
* </pre>
*
* @throws IOException
* An error occurred while reading
*
*/
/**
* <pre>
*
* </pre>
* @param path
* @param charset
* @throws IOException
*
*/
public void read(String path, String charset) throws IOException {
fis = new FileInputStream(path);
isr = new InputStreamReader(fis, charset);
while (fis.available() > 0) {
int length = isr.read(ch);
System.out.println(new String(ch));
}
}
public static void main(String[] args) {
try {
CharsetConvertion cc = new CharsetConvertion();
cc.convertionFile();
cc.convertionString();
cc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Method 3: java. nio. Charset
package com.qingshan.nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
/**
* <pre>
* use nio In the Charset The conversion character : The whole process is file reading -->byte--> decoding ( correct )--> coding --->byte--> Written to the file
* </pre>
* <hr Color="green" >
* </hr>
* 2012 Qingshan Group All rights reserved
* <hr Color="green" >
* </hr>
*
* @author thetopofqingshan
* @version 1.0.0
* @since JDK 1.5
* @date 2012-4-27
*/
public class CharsetConvertion {
private FileInputStream fis;// File input stream : Read the contents of the file
private FileChannel in;// File channel : two-way , The stream flows through it
private FileChannel out;// File channel : two-way , The stream flows through it
private FileOutputStream fos;// File output stream : Writes to a file
private ByteBuffer b = ByteBuffer.allocate(1024 * 3);// Sets the size of the cache
private Charset inSet;// Decoded character set
private Charset outSet;// Coded character set
private CharsetDecoder de;// decoder
private CharsetEncoder en;// The encoder
private CharBuffer convertion;// The middle character data
private ByteBuffer temp = ByteBuffer.allocate(1024 * 3);// Sets the size of the cache : temporary
private byte[] by = new byte[1024];
private InputStreamReader isr;
private char[] ch = new char[1024];
/**
* <pre>
*
* </pre>
*
* @param src
* @param dest
* @throws IOException
*
*/
public void convertionFile_nio(String src, String dest) throws IOException {
fis = new FileInputStream(src);
in = fis.getChannel();
fos = new FileOutputStream(dest);
out = fos.getChannel();
inSet = Charset.forName("gbk");
outSet = Charset.forName("utf-8");
de = inSet.newDecoder();
en = outSet.newEncoder();
while (fis.available() > 0) {
b.clear();// Remove tag
in.read(b); // Reads the file contents into the buffer : Mark the location from 0-b.capacity(),
// The end of reading is marked at 0-b.capacity() between
b.flip();// Adjust the tag , The next read starts at that location
convertion = de.decode(b);// Start coding
temp.clear();// Remove tag
temp = en.encode(convertion);
b.flip(); // Move the tag to the beginning of the buffer , And save all the data in it : Move the tag to the start 0
out.write(temp); // Writes the contents of the buffer to a file : Start fetching the data from the tag
}
}
/**
* <pre>
* Test whether the transcoding is successful , Specifies the character set to read the file
* </pre>
*
* @param src
* Full path to copied file
* @param charset Decoded character set
*
* @throws IOException An exception that occurs during a read
*
*/
public void read(String path, String charset) throws IOException {
fis = new FileInputStream(path);
isr = new InputStreamReader(fis, charset);
while (fis.available() > 0) {
int length = isr.read(ch);
System.out.println(new String(ch));
}
}
/**
* <pre>
* Close all streams or channels
* </pre>
*
*/
public void close() {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CharsetConvertion n = new CharsetConvertion();
try {
n.convertionFile_nio("C:/ Project progress tracking .txt", "C:/nio_write.txt");
// n.read("C:/nio_write.txt", "utf-8");// correct
// n.read("C:/nio_write.txt", "gbk");// The statement
} catch (IOException e) {
e.printStackTrace();
} finally {
n.close();
}
}
}
Thank you for reading, I hope to help you, thank you for your support to this site!