import java.io.*;
public class Test1 {
public static void main(String[] args) throws IOException{
CopyFile("F:\\1" + File.separator + "1.txt", "F:\\2");
}
/**
* 使用转换流
* @param sourceDirectory 源文件路径
* @param targetDirectory 要拷贝到的目标路径
* @throws IOException
*/
private static void CopyFile(String sourceDirectory , String targetDirectory) throws IOException {
File sourceFile = new File(sourceDirectory);
File targetFile = new File(targetDirectory + File.separator + sourceFile.getName());
//创建流
FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
//转换流
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
//创建临时储存区
char[] chars = new char[1024];
int length = inputStreamReader.read(chars);
while (length != -1){
outputStreamWriter.write(chars,0,length);
length = inputStreamReader.read(chars);
}
//关闭资源
inputStreamReader.close();
outputStreamWriter.flush();
outputStreamWriter.close();
}
}
IO操作中的转换流代码实现
最新推荐文章于 2022-09-09 14:53:14 发布