forked from tianshb/JavaLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseCharsets.java
More file actions
42 lines (34 loc) · 1.43 KB
/
Copy pathUseCharsets.java
File metadata and controls
42 lines (34 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.crow;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
/**
* Created by CrowHawk on 17/9/4.
*/
public class UseCharsets {
public static void main(String[] args) throws Exception{
String inputFile = "ReadAndShow.txt";
String outputFile = "sampleout.txt";
long inputLength = new File( inputFile ).length();
RandomAccessFile inf = new RandomAccessFile( inputFile, "r" );
RandomAccessFile outf = new RandomAccessFile( outputFile, "rw" );
FileChannel infc = inf.getChannel();
FileChannel outfc = outf.getChannel();
MappedByteBuffer inputData = infc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);
Charset latin1 = Charset.forName( "ISO-8859-1" );
CharsetDecoder decoder = latin1.newDecoder();//解码器
CharsetEncoder encoder = latin1.newEncoder();//编码器
CharBuffer cb = decoder.decode( inputData );//将字节数据解码为一组字符
// Process char data here
ByteBuffer outputData = encoder.encode( cb );//要写回数据,我们必须使用 CharsetEncoder 将它转换回字节
outfc.write( outputData );
inf.close();
outf.close();
}
}