大家好,最近我总结了一些文件拷贝的方法,今天我和大家分享一下这些方法。
我们要注意一点:我们只能对标准文件进行拷贝,不能对目录进行拷贝。
第一种:用InputStream和OutputStream对文件进行拷贝(用InputStream和OutputStream可以对任意的标准文件进行拷贝)
try (
//需要读取的原始文件输入流
InputStream inputStream = new FileInputStream("C:\\Users\\HP\\Desktop\\新建文件夹 (5)\\m.mp4");
//需要拷贝到的目标文件的输出流
OutputStream outputStream = new FileOutputStream("C:\\Users\\HP\\Desktop\\新建文件夹 (6)\\ok.mp4");
) {
System.out.println("开始拷贝");
//创建字节缓冲区
byte[] b = new byte[1024 * 1024];
//每次实际读取长度
int len = 0;
//循环读写
while ((len = inputStream.read(b)) != -1) {
//将字节数组从0开始,写入len长到输出流
outputStream.write(b, 0, len);
}
System.out.println("拷贝结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
第二种:用Reader和Writer对文件进行拷贝(用Reader和Writer只能对纯文本进行拷贝比如:txt文件、java文件、HTML文件、css文件等)
try (
Reader reader = new FileReader("C:\\Users\\HP\\Desktop\\新建文件夹 (5)\\新建文本文档.txt");
Writer writer = new FileWriter("C:\\Users\\HP\\Desktop\\新建文件夹 (6)\\ok.txt");
) {
System.out.println("开始拷贝");
char[] ch = new char[1024 * 1024];
int len = 0;
while ((len = reader.read(ch)) != -1) {
writer.write(ch, 0, len);
}
System.out.println("拷贝结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
第三种:用BufferedInputStream和BufferedOutputStream缓冲流对文件进行拷贝(BufferedInputStream和BufferedOutputStream缓冲流对InputStream和OutputStream进行包装提高了效率,可以对任意的标准文件进行拷贝)。
try (
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream("C:\\Users\\HP\\Desktop\\新建文件夹 (5)\\m.mp4"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream("C:\\Users\\HP\\Desktop\\新建文件夹 (6)\\ok1.mp4"));
) {
System.out.println("开始拷贝");
byte[] b = new byte[1024 * 1024];
int len = 0;
while ((len = bufferedInputStream.read(b)) != -1) {
bufferedOutputStream.write(b, 0, len);
}
System.out.println("拷贝结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
第四种:用BufferedReader和BufferedWriter缓冲流对文件进行拷贝(BufferedReader和BufferedWriter缓冲流对Reader和Writer进行包装提高了效率,同样它也只能对纯文本进行拷贝)。
try (
BufferedReader bufferedReader = new BufferedReader(
new FileReader("C:\\Users\\HP\\Desktop\\新建文件夹 (5)\\新建文本文档.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(
new FileWriter("C:\\Users\\HP\\Desktop\\新建文件夹 (6)\\ok1.txt"));
) {
/*第一种*/
// System.out.println("开始拷贝");
// char[] ch = new char[1024 * 1024];
// int len = 0;
// while ((len = bufferedReader.read(ch)) != -1) {
// bufferedWriter.write(ch, 0, len);
// }
// System.out.println("拷贝结束");
/*第二种*/
System.out.println("开始拷贝");
String src = "";
//每次读取一行数据
while ((src = bufferedReader.readLine()) != null) {
//写入读取的一行文本()
bufferedWriter.write(src);
//手动加入一个换行标记到文件,否则所有内容会在同一行显示
bufferedWriter.newLine();
//将缓冲区的数据强制输出到目标输出源
bufferedWriter.flush();
}
System.out.println("拷贝结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
第五种:用InputStreamReader和OutputStreamWriter转换流把InputStream和OutputStream转换成BufferedReader和BufferedWriter对文件进行拷贝(它也只能对纯文本进行拷贝)
try (
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream("C:\\Users\\HP\\Desktop\\新建文件夹 (5)\\新建文本文档.txt")));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("C:\\Users\\HP\\Desktop\\新建文件夹 (6)\\ok2.txt")));
) {
System.out.println("开始拷贝");
String line = "";
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
System.out.println("拷贝结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
第六种:用RandomAccessFile对文件进行拷贝(RandomAccessFile可以对任意的标准文件进行拷贝,RandomAccessFile即可以对文件进行读取,也可以对文件进行写入)
try (
//需要读取的原始文件输入流
RandomAccessFile read = new RandomAccessFile("C:\\Users\\HP\\Desktop\\新建文件夹 (5)\\m.mp4", "r");
//需要拷贝到的目标文件的输出流
RandomAccessFile write = new RandomAccessFile("C:\\Users\\HP\\Desktop\\新建文件夹 (6)\\ok3.mp4", "rw");
) {
System.out.println("开始拷贝");
byte[] b = new byte[1024 * 1024];
int len = 0;
while ((len = read.read(b)) != -1) {
write.write(b, 0, len);
}
System.out.println("拷贝完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}