Android 项目编码转换

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


/**
 * 主要实现将制定文件夹下的所有文本文件保存到UTF-8的文本编码
 * 
 * @author Rainbow
 * 
 */
public class ProjectCopyWithTextEncoder {


//目标路径
private static final String destDir = "F:"+File.separator ;

public static void main(String[] args) {


query(new File ("E:\\Android Catogorywc\\android_98player1.0\\App98playerUser5")) ;
}


/**
* 将指定的文本保存到指定的编码格式中
* @param resFile 源文件实例
* @param destFile 目标位置
*/
private static void toUtf8TextFile(File resFile , File destFile) {
try {
FileInputStream fis = new FileInputStream(resFile);
InputStreamReader isr = new InputStreamReader(fis , "GBK");
FileOutputStream fos = new FileOutputStream(destFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");//
int s;
while ((s = isr.read()) != -1) {
osw.write(s);
}
osw.flush();
osw.close();
isr.close();
System.out.println("从"+resFile.getParent()+" 拷贝到 :"+destFile.getPath());
} catch (IOException e) {
e.printStackTrace();
}
}


/**
* 传入指定文件目录
* @param file
*/
private static void query (File file) {

if(file.isDirectory()) {

File[] files = file.listFiles() ;

int len = files.length ;

for (int i = 0 ; i < len ; i ++) {

if(files[i].isDirectory()) {

query( files[i] );

} else {

doWorkFile(files[i]) ;

}

}
} else {

doWorkFile(file) ;
}

}

/**
* 只针对java文件和xml文件
* @param file
*/
private static void doWorkFile (File  file) {

File dir = new File(destDir + filterPackage(file)) ;

if(!dir.exists()) {

dir.mkdirs() ;
System.out.println("create folder : "+dir.getPath());

}
if(file.getPath().endsWith(".java") || file.getPath().endsWith(".xml")) {

toUtf8TextFile(file , new File(dir, file.getName())) ;

} else {

copyUniversalFile(file , new File(dir, file.getName())) ;

}

}
private static void copyUniversalFile (File resFile , File destFile) {

try {
FileInputStream fin = new FileInputStream(resFile);
byte data[] =  new byte[fin.available()];

FileOutputStream fout = new FileOutputStream(destFile) ;
fout.write(data) ;

fout.flush() ;
fout.close() ;

fin.close() ;

} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 筛选出包
*/
public static String filterPackage (File file) {

return file.getParentFile().getPath().split(":")[1] ;

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值