35、IO流——拷贝

二进制文件的拷贝

图片、音频、视频等,只能用字节流

拷贝二进制文件有4种方式:
1、字节缓冲流一次读写一个字节数组(效果最佳)

public static void main(String[] args) throws IOException {
String srcString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy\\a.txt";
String destString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy22222\\b.txt";

File srcFile = new File(srcString);
File destFile = new File(destString);

method(srcFile, destFile);
}

public static void method(File srcFile, File destFile) throws IOException {
	BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
	BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));

byte[] bs = new byte[1024];
int length = 0;
while ((length = bufferedInputStream.read(bs)) != -1) {
	bufferedOutputStream.write(bs, 0, length);
}

bufferedInputStream.close();
bufferedOutputStream.close();
}

2、字节缓冲流一次读写一个字节

public static void main(String[] args) {
	String srcStr = "D:\\1.txt";
	String tagStr = "D:\\IOTest\\2.txt";

	File f1 = new File(srcStr);
	File f2 = new File(tagStr);

	copy(f1, f2);
}
	
public static void copy(File f1,File f2) {
	BufferedInputStream bis=null;
	BufferedOutputStream bos=null;
	try {
		bis = new BufferedInputStream(new FileInputStream(f1));
		bos = new BufferedOutputStream(new FileOutputStream(f2));
		int length=0;
		while ((length=bis.read())!=-1) {
			bos.write(length);
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

3、基本字节流一次读写一个字节数组

public static void main(String[] args) {
String srcStr="D:\\1.txt";
	String tagStr="D:\\IOTest\\2.txt";
		
	copy(srcStr, tagStr);
}
	
public static void copy(String srcStr,String tagStr) {
	FileInputStream fis=null;
	FileOutputStream fos=null;
	try {
		fis = new FileInputStream(srcStr);
		fos = new FileOutputStream(tagStr);
		byte[] bs=new byte[1024];
		int length=0;
		while ((length=fis.read(bs))!=-1) {
			fos.write(bs,0,length);
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

4、基本字节流一次读写一个字节

public static void main(String[] args) {
	String srcStr="D:\\1.txt";
	String tagStr="D:\\IOTest\\2.txt";
		
	copy(srcStr,tagStr);
}
	
public static void copy(String srcStr, String tagStr) {
	FileInputStream fis=null;
	FileOutputStream fos=null;
	try {
		fis = new FileInputStream(srcStr);
		fos = new FileOutputStream(tagStr);
		int length=0;
		while ((length=fis.read())!=-1) {
			fos.write(length);
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (fis!=null||fos!=null) {
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

文本文件(*.txt文件)的拷贝

用字节流和字符流都可以,但是推荐用字符流

拷贝文本文件,有5种方式:
1、字符缓冲流一次读写一个字符串(效果最佳)

public static void main(String[] args)  throws IOException{
	String srcString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy\\a.txt";
	String destString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy22222\\b.txt";
	method1(srcString,destString);
}

public static void method1(String srcString,String destString) throws IOException{
	BufferedReader bufferedReader = new BufferedReader(new FileReader(srcString));
	BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destString));

	String line = null;
	while ((line = bufferedReader.readLine())!= null) {
		bufferedWriter.write(line);
		bufferedWriter.newLine();
		bufferedWriter.flush();
	}

	bufferedReader.close();
	bufferedWriter.close();	
}

2、字符缓冲流一次读写一个字符数组

public static void main(String[] args) {
	String srcStr="D:\\1.txt";
	String tagStr="D:\\IOTest\\2.txt";
	
	copy(srcStr,tagStr);
}

public static void copy(String srcStr, String tagStr) {
	BufferedReader br=null;
	BufferedWriter bw=null;
	try {
		br = new BufferedReader(new FileReader(srcStr));
		bw = new BufferedWriter(new FileWriter(tagStr));
		char[] cs=new char[1024];
		int length=0;
		while ((length=br.read(cs))!=-1) {
			bw.write(new String(cs, 0, length));
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br!=null||bw!=null) {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

3、字符缓冲流一次读写一个字符

public static void main(String[] args) {
	String srcStr="D:\\1.txt";
	String tagStr="D:\\IOTest\\2.txt";
	copy(srcStr,tagStr);
}

public static void copy(String srcStr, String tagStr) {
	BufferedReader br=null;
	BufferedWriter bw=null;
	try {
		br = new BufferedReader(new FileReader(srcStr));
	bw = new BufferedWriter(new FileWriter(tagStr));
		int length=0;
		while ((length=br.read())!=-1) {
			bw.write((char)length);
	}
	} catch (FileNotFoundException e) {
	e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br!=null||bw!=null) {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

4、基本字符流一次读写一个字符数组

public static void main(String[] args) {
	String srcStr="D:\\1.txt";
	String tagStr="D:\\IOTest\\2.txt";
	
	copy(srcStr, tagStr);
}

public static void copy(String srcStr, String tagStr) {
	InputStreamReader isr=null;
	OutputStreamWriter osw=null;
	try {
		isr = new InputStreamReader(new FileInputStream(srcStr),"GB2312");
		osw = new OutputStreamWriter(new FileOutputStream(tagStr),"GB2312");
		char[] cs=new char[1024];
		int length=0;
		while ((length=isr.read(cs))!=-1) {
			osw.write(new String(cs, 0, length));
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (isr!=null) {
			try {
				isr.close();
				osw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

5、基本字符流一次读写一个字符

public static void main(String[] args) {
	String srcStr="D:\\1.txt";
	String tagStr="D:\\IOTest\\2.txt";
	
	copy(srcStr, tagStr);
}
	
public static void copy(String srcStr, String tagStr) {
	InputStreamReader isr=null;
	OutputStreamWriter osw=null;
	try {
		isr = new InputStreamReader(new FileInputStream(srcStr),"GB2312");
		osw = new OutputStreamWriter(new FileOutputStream(tagStr),"GB2312");
		int length=0;
		while ((length=isr.read())!=-1) {
			osw.write(length);
			osw.flush();
		}
	} catch (FileNotFoundException e) {
	e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (isr!=null) {
			try {
				isr.close();
				osw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
### Java 中使用 IO 实现文件拷贝Java 中,可以通过字节或字符来完成文件的复制工作。对于二进制文件(如图像、视频),应采用 `FileInputStream` 和 `FileOutputStream` 来执行读取和写入操作;而对于纯文本文件,则可以选择更高效的 `FileReader` 和 `FileWriter` 进行处理[^4]。 下面是一个利用字节进行文件复制的具体实例: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) { String sourceFilePath = "source.txt"; String destinationFilePath = "destination.txt"; try (FileInputStream fis = new FileInputStream(sourceFilePath); FileOutputStream fos = new FileOutputStream(destinationFilePath)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } System.out.println("文件已成功复制!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 此代码片段展示了如何创建两个文件对象——一个是用于源文件的输入 (`FileInputStream`) ,另一个是目标文件的输出(`FileOutputStream`). 接着定义了一个缓冲区数组,在循环体内不断从输入中读取数据并将其写入到输出直到整个文件被完全传输完毕[^1]. 当涉及到大容量的数据或者追求更高的性能时,建议增加缓存大小以减少磁盘I/O次数,并考虑关闭自动刷新功能以便手动控制何时将内容刷入目的地文件.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值