java中生成文件的编码问题

      今天下载了很多txt格式的书,准备放到手机里无聊的时候看看。可是在手机里显示竟然是乱码,原来txt默认文件编码是ASCII码,于是我把其中一个文件改成unicode码,拷到手机里居然就可以 正常显示。但是下载几十部书,每部书大概有20个txt文件,数量比较多,手工改的话估计吃不消,我就写了个java程序试着能否把文件默认格式转成unicode码格式。程序如下:
/**
 * 给定一个源文件路径或者File和目标路径,把源文件路径下的的所有txt文件和文件夹拷贝到目标路径下
 * 并且可以按照给定的编码输出txt文件
 * 
@author  myyate
 *
 
*/
public   class  Converter {
    
public  Converter(String inPath, String outPath)  throws  Exception {
    
this (inPath, outPath, DEFAULT_ENCODING);
    }
    
public  Converter(String inPath, String outPath, String encoding)  throws  Exception {
    
this ( new  File(inPath),  new  File(outPath), encoding);
    }
    
public  Converter(File inFile, File outFile)  throws  Exception {
    
this (inFile, outFile, DEFAULT_ENCODING);
    }
    
public  Converter(File inFile, File outFile, String encoding)  throws  Exception {
    
this .inFile  =  inFile;
    
this .outFile  =  outFile;
    
if ( ! outFile.exists()) {
        outFile.mkdir();
    }
    
this .encoding  =  encoding;
    }
    
    
public   void  convert()  throws  Exception {
    copyFiles(inFile, outFile);
    }
    
    
private   void  copyFiles(File inDirectory, File outDirectory)  throws  Exception {
    
for (File file : inDirectory.listFiles( new  TxtFileFilter())) {
        File outFile 
=   new  File(outDirectory.getAbsolutePath(), file.getName());
        
if (file.isFile()) {
        
// 如果是文件,就把文件拷贝到目标文件夹中
        copySingleFile(file, outFile);
        } 
else   if (file.isDirectory()) {
        
if ( ! outFile.exists()) {
            outFile.mkdir();
            System.out.println(
" Create directory [ "   +  outFile.getAbsolutePath()  +   " ] " );
        }
        copyFiles(file, outFile);
        }
    }
    }

    
private   void  copySingleFile(File src, File des)  throws  Exception {
    
if ( ! des.exists()) {
        des.createNewFile();
    }
    String encodingUse 
=  ((encoding  ==   null ?  DEFAULT_ENCODING : encoding);
    Reader in 
=  
        
new  BufferedReader( new  InputStreamReader( new  FileInputStream(src)));
    
// 设定输出文件的编码
    Writer out  =  
        
new  BufferedWriter( new  OutputStreamWriter( new  FileOutputStream(des), encodingUse));

    
char [] buffer  =   new   char [ 4096 ];
    
int  readBytes  =   - 1 ;
    
while ((readBytes  =  in.read(buffer))  !=   - 1 ) {
        out.write(buffer, 
0 , readBytes);
    }
    out.flush();
    
    out.close();
    in.close();
    System.out.println(
" Copy file from [ "   +  src.getAbsolutePath()  +   " ] to [ "   +  des.getAbsolutePath()  +   " ] " );
    }

    
    
class  TxtFileFilter  implements  FileFilter {

    
public   boolean  accept(File pathname) {
        
if (pathname.isDirectory()) {
        
return   true ;
        }
        
        String absolutePath 
=  pathname.getAbsolutePath();
        String suffix 
=  absolutePath.substring(absolutePath.length()  -   3 );
        
// 只拷贝txt文件
         if ( " txt " .equalsIgnoreCase(suffix)) {
        
return   true ;
        }
        
        
return   false ;
    }
    }
  
    
private  File inFile;     // 输入路径
     private  File outFile;     // 输出路径
     private  String encoding;     // 输出文件编码
     private   static  String DEFAULT_ENCODING  =   " GBK " ;
}

测试程序如下:

public   class  Test {

    
static  String IN_PATH  =   " F:/Novel " ;
    
static  String OUT_PATH  =   " F:/NovelConvert " ;
    
    
public   static   void  main(String[] args)  throws  Exception {
    Converter convert 
=   new  Converter(IN_PATH, OUT_PATH,  " Unicode " );
    convert.convert();
    }
}

程序是可以正常执行了,并且也转码了,不过不幸的是转成后的文件格式不是unicode,而是unicode big endian,拷贝到手机里还是乱码,nnd,现在也不管手机可否看了,我想问大家一下,如何修改才可以把txt文件改成unicode格式呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值