windows到linux下面文本从ansi转utf-8(java调用shell命令,java文件复制)
环境:centos6.5 eclipse
当文本从windows下面直接拷贝进linux的时候,文本内容显示是乱码,这是因为windows下面text默认为ansi,linux无法识别,所以我们要转码为utf-8,程序如下:
import java.io.File;
public class gbk_utf8_00 {
public static void deleteAll(String sStr, String dStr) throws Exception {
File in = new File(sStr);
File out = new File(dStr);
if (!out.exists())
out.mkdirs();
File[] file = in.listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
if (file[i].getName().endsWith(".txt")
|| file[i].getName().endsWith(".java")
|| file[i].getName().endsWith(".TXT")) {
String cmd = "iconv -f gbk -t utf-8 " + file[i] + " > "
+ dStr + "/" + file[i].getName();//没有进行转意字符的控制
Process ps = Runtime.getRuntime().exec(
new String[] { "sh", "-c", cmd });
ps.waitFor();
} else {
String cmd2 = "cp " + file[i] + " " + dStr + "/"
+ file[i].getName();//没有进行转意字符的控制
Process ps = Runtime.getRuntime().exec(
new String[] { "sh", "-c", cmd2 });
System.out.println(cmd2);
ps.waitFor();
}
} else
deleteAll(sStr + "/" + file[i].getName(),
dStr + "/" + file[i].getName());
}
}
public static void main(String[] args) throws Exception {
deleteAll("/home/wjx/test"