首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

TXT资料编码转换例子

2012-09-12 
TXT文件编码转换例子import java.io.Fileimport java.io.RandomAccessFileimport java.nio.ByteBufferi

TXT文件编码转换例子
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;  
 
public class TranslateCharset {  
  static public void main(String args[]) throws Exception {  
    String inFilename = "inputFileName.txt";  
    String inFileCharsetName = "InputFileCharSetName";  
    String outFilename = "outputFileName.txt";  
    String outFileCharsetName = "OutputFileCharSetName";  
 
    File infile = new File(inFilename);  
    File outfile = new File(outFilename);  
 
    RandomAccessFile inraf = new RandomAccessFile(infile, "r");  
    RandomAccessFile outraf = new RandomAccessFile(outfile, "rw");  
 
    FileChannel finc = inraf.getChannel();  
    FileChannel foutc = outraf.getChannel();  
 
    MappedByteBuffer inmbb = finc.map(FileChannel.MapMode.READ_ONLY, 0, (int) infile.length());  
 
    Charset inCharset = Charset.forName(inFileCharsetName);  
    Charset outCharset = Charset.forName(outFileCharsetName);  
 
    CharsetDecoder inDecoder = inCharset.newDecoder();  
    CharsetEncoder outEncoder = outCharset.newEncoder();  
 
    CharBuffer cb = inDecoder.decode(inmbb);  
    ByteBuffer outbb = outEncoder.encode(cb);  
 
    foutc.write(outbb);  
 
    inraf.close();  
    outraf.close();  
  }  
}  

热点排行