文件字节流复制文件
- 因为引用字节流是直接使用二进制,所以文件字节流非常通用,可以用来操作字符的文档 ,还可以操作任何的其他类型文件,例如:图片、压缩包等等。
- 编写一个程序,吧一个文件复制到指定文件夹下
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyDemo {
public static void copyTest(String inPath,String outPath) throws Exception {
FileInputStream in = new FileInputStream(inPath);
FileOutputStream out = new FileOutputStream(outPath);
byte[] b = new byte[100];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b,0,len);
}
out.flush();
out.close();
in.close();
}
public static void main(String[] args) throws Exception {
CopyDemo.copyTest("D:\\JAVA\\Java 基础入门\\IO 流\\test\\t1.txt",
"D:\\JAVA\\Java 基础入门\\IO 流\\test\\demo\\t1.txt");
}
}