参考:https://blog.csdn.net/qq_43470725/article/details/121283862
https://blog.csdn.net/Enjoy96/article/details/78650182
详情参考:io流
1、文本io流的复制 代码演示:
package com.example.io.fileIo.copy.charCopy;
import java.io.*;
public class CharCopyTest01 {
public static void main(String[] args) {
CharCopyTest01 charCopyTest01 = new CharCopyTest01();
String path = charCopyTest01.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFile(path,"test","\\test.txt");
// 创建要复制到目标的文件夹及文件:
File fileCopy = createFile(path, "test", "\\testCopy.txt");
// 复制文件
try {
// 一次读一个字符数组
// copyFile(file, fileCopy);
// 一次读一个字符
// copyFileByOne(file, fileCopy);
// 一次读一行
// copyFileByLine(file,fileCopy);
//基本字符流读取一个字符数组(效率低)
// copyFileByBasic(file, fileCopy);
//基本字符流读取一个字符(效率低)
// copyFileByBasicOne(file, fileCopy);
// 采用打印流PrintWriter的println()方法进行文件的复制
copyFileByPrintWriter(file, fileCopy);
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
public static File createFile(String path,String filePath,String fileName){
// 创建文件夹
path = path + filePath;
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + fileName;
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
// 缓冲字符流一次读取一个字符数组
public static void copyFile(File pathSource,File pathCopyRoot) throws IOException {
// 封装数据源:
// 创建一个文件字节输入流对象
BufferedReader br = new BufferedReader(new FileReader(pathSource));
// 封装目的地
// 创建文件输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopyRoot));
// 一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1){
// 一次读一个字符数组,使用输出流给testCopy.txt文件写一个字符数组
bw.write(chs, 0, len);
// 刷新流
bw.flush();
}
// 释放资源
br.close();
bw.close();
}
// 缓冲字符流一次读取一个字符
public static void copyFileByOne(File pathSource,File pathCopyRoot) throws IOException {
// 封装数据源:
// 创建一个文件字节输入流对象
BufferedReader br = new BufferedReader(new FileReader(pathSource));
// 封装目的地
// 创建文件输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopyRoot));
int ch = 0;
while ((ch = br.read()) != -1){
// 一次读一个字符数组,使用输出流给testCopy.txt文件写一个字符数组
bw.write(ch);
// 刷新流
bw.flush();
}
// 释放资源
br.close();
bw.close();
}
// 缓冲字符流一次读一行:
public static void copyFileByLine(File pathSource,File pathCopyRoot) throws IOException {
// 封装数据源:
// 创建一个文件字节输入流对象
BufferedReader br = new BufferedReader(new FileReader(pathSource));
// 封装目的地
// 创建文件输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopyRoot));
// 使用BufferedReader中的特有功能public String readLine() 一次读取一行
String str = null;
while ((str = br.readLine()) != null){
// 一次读一行,使用输出流给b.txt文件写入此行字符串
bw.write(str);
//BufferedWriter中的特有功能public void newLine() 写入一个分行符
bw.newLine();
// 刷新流
bw.flush();
}
// 释放资源
br.close();
bw.close();
}
//基本字符流读取一个字符数组(效率低)
public static void copyFileByBasic(File pathSource,File pathCopyRoot) throws IOException {
FileReader fr = new FileReader(pathSource);
FileWriter fw = new FileWriter(pathCopyRoot);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1){
fw.write(chs,0,len);
fw.flush();
}
fr.close();
fw.close();
}
//基本字符流读取一个字符(效率低)
public static void copyFileByBasicOne(File pathSource,File pathCopyRoot) throws IOException {
FileReader fr = new FileReader(pathSource);
FileWriter fw = new FileWriter(pathCopyRoot);
int ch = 0;
while ((ch = fr.read()) != -1){
fw.write(ch);
fw.flush();
}
fr.close();
fw.close();
}
// 采用打印流PrintWriter的println()方法进行文件的复制
public static void copyFileByPrintWriter(File pathSource,File pathCopyRoot) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(pathSource));
PrintWriter pw = new PrintWriter( new FileWriter(pathCopyRoot),true);// 此种构造方式可以自动刷新
// 读取数据
String line = null;
while ((line = br.readLine()) != null){
// 写入数据,读取一行并启动自动换行
pw.println(line);
}
// 释放资源
br.close();
pw.close();
}
}
2、文件(图片、视频等)的复制 代码演示:
package com.example.io.fileIo.copy.byteCopy;
import java.io.*;
public class ByteCopyTest01 {
public static void main(String[] args) {
ByteCopyTest01 byteCopyTest01 = new ByteCopyTest01();
String path = byteCopyTest01.getPath();
path = getPath01(path);
System.out.println(path);
// 创建文件夹和文件并返回File:
File file = createFlode(path,"test");
String jpgPathSource = "D:\\2.jpg";
try {
String jpgPathRoot = file.getCanonicalPath().toString();
System.out.println(jpgPathRoot);
jpgPathRoot = jpgPathRoot + "\\2copy.jpg";
// 缓冲字节流一次读一个字节数组
// copyFileByBuffer(jpgPathSource, jpgPathRoot);
// 缓冲字节流一次读一个字节
// copyFileByBufferOne(jpgPathSource, jpgPathRoot);
//采用基本字节输入流,一次读一个字节数组
// copyFileByBasic(jpgPathSource, jpgPathRoot);
// 采用基本字节输入流,一次读一个字节
// copyFileByBasicOne(jpgPathSource, jpgPathRoot);
// 采用打印流PrintStream的writer()方法进行图片的复制
copyFileByPrintStream(jpgPathSource, jpgPathRoot);
} catch (IOException e) {
e.printStackTrace();
}
}
// 得到该类的路径
public String getPath(){
String path = this.getClass().getResource("").getPath();
return path;
}
public static String getPath01(String pathIn){
pathIn = pathIn.substring(1,pathIn.length());
pathIn = pathIn.replace("/", "\\");
System.out.println("pathIn==="+pathIn);
return pathIn;
}
// 创建文件
public static File createFile(String path, String filePath, String fileName){
// 创建文件夹
path = path + filePath;
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
// 创建文件
path = path + fileName;
System.out.println("path---last===="+path);
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
// 创建文件夹
public static File createFlode(String path, String filePath){
// 创建文件夹
path = path + filePath;
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
return file;
}
// 缓冲字节流一次读一个字节数组
public static void copyFileByBuffer(String pathSource,String pathRoot) throws IOException {
//获取当前的系统时间
long startTime = System.currentTimeMillis() ;
// 封装数据源:
// 创建一个文件字节输入流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));
// 封装目的地
// 创建文件输出流对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathRoot));//如果没有文件,会自动创建
// 使用高效字节输入流,一次读取一个字节数组
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
// 一次读一个字节数组,使用输出流给Marco.jpg文件写一个字节数组
bos.write(bytes,0,len);
// 刷新流
bos.flush();
}
// 释放资源
bis.close();
bos.close();
// 时间测试
long endTime = System.currentTimeMillis() ;
System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
}
// 缓冲字节流一次读一个字节
public static void copyFileByBufferOne(String pathSource,String pathRoot) throws IOException {
//获取当前的系统时间
long startTime = System.currentTimeMillis() ;
// 封装数据源:
// 创建一个文件字节输入流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));
// 封装目的地
// 创建文件输出流对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathRoot));//如果没有文件,会自动创建
// 使用高效字节输入流,一次读取一个字节
int by;
while ((by = bis.read()) != -1){
// 一次读一个字节数组,使用输出流给Marco.jpg文件写一个字节数组
bos.write(by);
// 刷新流
bos.flush();
}
// 释放资源
bis.close();
bos.close();
// 时间测试
long endTime = System.currentTimeMillis() ;
System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
}
//采用基本字节输入流,一次读一个字节数组
public static void copyFileByBasic(String pathSource,String pathRoot) throws IOException {
// 获取当前的系统时间
long startTime = System.currentTimeMillis();
// 封装数据源:
// 创建一个文件字节输入流对象
FileInputStream fis = new FileInputStream(pathSource);
// 封装目的地
// 创建文件输出流对象
FileOutputStream fos = new FileOutputStream(pathRoot);
// 使用基本字节输入流,一次读取一个字节数组
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1){
// 一次读一个字节数组,使用输出流给Marco.jpg文件写一个字节数组
fos.write(bytes,0,len);
// 刷新流
fos.flush();
}
// 释放资源
fis.close();
fos.close();
// 时间测试
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒"); // 1249毫秒
}
// 采用基本字节输入流,一次读一个字节
public static void copyFileByBasicOne(String pathSource,String pathRoot) throws IOException {
// 获取当前的系统时间
long startTime = System.currentTimeMillis();
// 封装数据源:
// 创建一个文件字节输入流对象
FileInputStream fis = new FileInputStream(pathSource);
// 封装目的地
// 创建文件输出流对象
FileOutputStream fos = new FileOutputStream(pathRoot);
// 使用基本字节输入流,一次读取一个字节
int by = 0;
while ((by = fis.read()) != -1){
// 一次读一个字节,使用输出流给02test.jpg文件写一个字节
fos.write(by);
// 刷新流
fos.flush();
}
// 释放资源
fis.close();
fos.close();
// 时间测试
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
}
// 采用打印流PrintStream的writer()方法进行图片的复制
public static void copyFileByPrintStream(String pathSource,String pathRoot) throws IOException {
// 获取当前的系统时间
long startTime = System.currentTimeMillis();
// 封装数据源
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));
// 封装目的地,使用字符打印流对象并启动自动刷新功能
PrintStream ps = new PrintStream(new FileOutputStream(pathRoot),true); // 此种构造方式可以自动刷新
// 读取数据,一次读取一个字节数组
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes)) != -1){
// 写入数据,一次写一个字节数组
ps.write(bytes,0,len);
}
// 释放资源
bis.close();
ps.close();
// 时间测试
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
}
}