Android 项目编码转换

来源:互联网 发布:淘宝上架数量限制 编辑:程序博客网 时间:2024/05/24 01:10
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