今天这篇文章主要写的是文件简单的读写复制,从一个文件中读出再写入另外一个文件中。
package com.lsl.readAndwrite;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File startfile = new File("D:\\book\\1.txt");// 定义一个源文件的对象
File goalfile = new File("D:\\book\\3.txt");// 定义一个接收文件的对象
try {FileInputStream fis = new FileInputStream(startfile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(new FileOutputStream(goalfile));
byte[] bytes = new byte[1024];
while (fis.read(bytes) != -1) {bos.write(bytes);
}
bos.flush();
bos.close();
fis.close();} catch (FileNotFoundException e) {e.printStackTrace();
System.out.println("D:\\book\\1.txt 不存在");
System.out.println(-1);
} catch (IOException e) {e.printStackTrace();
}
}
}