文件内容复制
- 创建两个txt文件,(inputstream.txt,outputstream.txt)
2. 在inputstream.txt中写入文字(outputstream.txt 中不写任何内容)
- 进行文件内容拷贝
package com.gao.xi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestAllEng {
public static void main(String[] args) {
/*
* 1.获取当前类的所在路径
* 2.获取两个文件的路径
* 3.声明FileInputStream和FileOutStream变量
* 4.实例化文件输出输入流
* 5.读写文件
* 6.关闭流
*/
// * 1.获取当前类的所在路径
String path = "src/com/gao/xi/";
// * 2.获取两个文件的路径
String inpath = path + "inputstream.txt";
String outpath = path + "outputstream.txt";
// * 3.声明FileInputStream和FileOutStream变量
FileInputStream fin = null;
FileOutputStream fout = null;
try {
// * 4.实例化文件输出输入流
fin = new FileInputStream(inpath);
fout = new FileOutputStream(outpath);
// * 5.读写文件
byte[] buff = new byte[1024];
int len = 0;
while ((len = fin.read(buff)) != -1) {
fout.write(buff, 0, len);
System.out.println(new String(buff, 0, len));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// * 6.关闭流
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}