Java 中用字节流拷贝文件:
只需用输入流和输出流分别进行读取和写入即可
try {
File f = new File("D:/test/2.txt"); //参考文件位置
File f1 = new File("D:/test/3.txt"); //拷贝目标文件
InputStream fis = new FileInputStream(f); //输入流 inputStream 是所有输入流的对象
OutputStream fos = new FileOutputStream(f2); //输出流 输出到f2
byte[] bs = new byte[1024];
int len = 0; //实际读取个数
while( (len = fis.read(bs))>0 ){ //read返回 读取的字节数
fos.write(bs,0,len); //从0到len位置写入到数组bs中
}
fos.close();
fis.close();
}
catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}