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();
			}
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值