public class NioTest13 {
public static void main(String[] args) throws Exception {
String inputFile = "NioTest13_in.txt";
String outputFile = "NioTest13_out.txt";
RandomAccessFile inputRandomAccessFile = new RandomAccessFile(inputFile, "r");
RandomAccessFile outputRandomAccessFile = new RandomAccessFile(outputFile, "rw");
long inputLength = new File(inputFile).length();
FileChannel inputFileChannel = inputRandomAccessFile.getChannel();
FileChannel outputFileChannel = outputRandomAccessFile.getChannel();
MappedByteBuffer inputData = inputFileChannel.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);
Charset charset = Charset.forName("iso-8859-1");//utf-8
CharsetEncoder encoder = charset.newEncoder();
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(inputData);
ByteBuffer byteBuffer = encoder.encode(charBuffer);
outputFileChannel.write(byteBuffer);
inputRandomAccessFile.close();
outputRandomAccessFile.close();
}
}
网友评论