亲测可用,望采纳,不懂追问
public class FileCopy {
public static void main(String[] args) throws IOException{
// 构建源文件
File file = new File("E:" + File.separator + "text.txt");
OutputStream out = null;
// 根据文件创建文件的输出流
out = new FileOutputStream(file);
String message = "hello Java";
// 把内容转换成字节数组
byte[] data = message.getBytes();
// 向文件写入内容
out.write(data);
// 构建目标文件
File fileCopy = new File("E:" + File.separator + "HelloWorld.txt");
InputStream in = null;
// 目标文件不存在就创建
if (!(fileCopy.exists())) {
fileCopy.createNewFile();
}
// 源文件创建输入流
in = new FileInputStream(file);
// 目标文件创建输出流
OutputStream out2 = null;
out2 = new FileOutputStream(fileCopy, true);
// 创建字节数组
byte[] temp = new byte[1024];
int length = 0;
// 源文件读取一部分内容
while ((length = in.read(temp)) != -1) {
// 目标文件写入一部分内容
out2.write(temp, 0, length);
}
// 关闭文件输入输出流
in.close();
out.close();
out2.close();
}
}