-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseCharsets.java
More file actions
39 lines (28 loc) · 1005 Bytes
/
UseCharsets.java
File metadata and controls
39 lines (28 loc) · 1005 Bytes
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
// $Id$
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class UseCharsets
{
static public void main( String args[] ) throws Exception {
String inputFile = "samplein.txt";
String outputFile = "sampleout.txt";
RandomAccessFile inf = new RandomAccessFile( inputFile, "r" );
RandomAccessFile outf = new RandomAccessFile( outputFile, "rw" );
long inputLength = new File( inputFile ).length();
FileChannel inc = inf.getChannel();
FileChannel outc = outf.getChannel();
MappedByteBuffer inputData =
inc.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 );
outc.write( outputData );
inf.close();
outf.close();
}
}