JAVA 文件编码转换

这是我的第一篇文章,主要用JAVA实现文件编码的转换,方便window到linux文件拷贝

下面是实现的方法:

Gbk_To_Utf8.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Gbk_To_Utf8 {
public static void ProcessFile(String inputPath,String outputPath
,String InputFormat,String OuputFormat) throws IOException{
FileAllFileOnPath file = new FileAllFileOnPath();
ArrayList path;
file.tree(new File(inputPath));
//保存文件目录下的所有文件
path = file.getfile();
for(int i = 0;i<path.size();i++){

//打开文件,使用InputFormat指定的格式
File files = new File(path.get(i).toString());
InputStreamReader read = new InputStreamReader(
new FileInputStream(files),InputFormat);
BufferedReader reader = new BufferedReader(read);
String line;
String temp = new String();
while ((line = reader.readLine()) != null) {
temp += line+"\n";
}//写入文件,使用OutputFormat指定的格式
BufferedWriter writer = null;
if(!outputPath.endsWith("/")){
outputPath = outputPath+"/";
}
FileOutputStream writerStream = new FileOutputStream(
outputPath + files.getName());
writer = new BufferedWriter(
new OutputStreamWriter(writerStream, OuputFormat));  
writer.write(temp);
writer.close();
}
}
}
class FileAllFileOnPath {

private static ArrayList list = new ArrayList<String>();
private static long filenum;

public static ArrayList getfile(){//返回文件列表
return list;
}

//显示目录的方法
public static void tree(File f){
//判断传入对象是否为一个文件夹对象
if(!f.isDirectory()){
System.out.println("请检查路径是否有误!!");
}
else{
File[] t = f.listFiles();
for(int i=0;i<t.length;i++){
//判断文件列表中的对象是否为文件夹对象,如果是则执行tree递归,直到把此文件夹中所有文件输出为止
if(t[i].isDirectory()){
tree(t[i]);
}
else{
// System.out.println(t[i].getPath());
list.add(t[i].getPath());
filenum++;
}
}
}
}
}


下面是测试代码:

Test.java

import java.io.IOException;


public class Test {
public static void main(String[] args) throws IOException{
Gbk_To_Utf8.ProcessFile("C:/Users/xy_lr/Desktop/80原文",
"D:/80/", "GBK", "utf-8");
}
}


这是第一次写文章,有什么问题希望大家帮我指出来,谢谢


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值