前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java学习---------IO流学习---文件流inputstream outputstream(二)

java学习---------IO流学习---文件流inputstream outputstream(二)

作者头像
wust小吴
发布2019-07-05 11:02:45
5580
发布2019-07-05 11:02:45
举报
文章被收录于专栏:风吹杨柳

利用文件输入流和输出流实现文件的复制,

下面的方法是:10字节10字节的复制,并且会把目标文件原来的内容给覆盖掉,如果目标文件原来有内容的话。

代码语言:javascript
复制
	// 从指定的文件中读取内容,并写入到指定的文件内(相当于文件的复制)
	@Test
	public void testFileInputOutputStream() {
		File src = new File("file/iotest.txt");//源文件
		File des = new File("file/outputstream.txt");//目标文件

		FileInputStream fis = null;//输入流,读
		FileOutputStream fos = null;//输出流,写

		try {
			fis = new FileInputStream(src);//构造
			fos = new FileOutputStream(des);

			byte[] b = new byte[10];//字节数组
			int len;
			while ((len = fis.read(b)) != -1) {//从源文件中的输入流中读
				fos.write(b, 0, len);//每读10字节就写进去,写到目标文件
				//特别注意:这种写法将会把目标文件的原来内容给覆盖掉
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null)
					fos.close();//关闭写操作
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fis != null)
					fis.close();//关闭读操作
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

把上面的测试函数变成一个通用的函数就是:

代码语言:javascript
复制
	public void copyFile(String src, String des) {
		File file1 = new File(src);
		File file2 = new File(des);

		FileInputStream fis = null;
		FileOutputStream fos = null;

		try {
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);

			byte[] b = new byte[1024];
			int len;
			while ((len = fis.read(b)) != -1) {
				fos.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

注意这里没有判断文件是否存在,具体的可以参考上篇文章。

代码语言:javascript
复制
	/**通过使用FileReader和FileWriter实现文件的复制
	 * Reader和Writer是一个字符流,也就是16bit
	 * @param src
	 * @param des
	 */
	public void copyFile(String src, String des) {
		File file1 = new File(src);
		File file2 = new File(des);

		FileReader fr = null;
		FileWriter fw = null;

		try {
			fr = new FileReader(file1);//构造
			fw = new FileWriter(file2);

			char[] b = new char[1024];//字符数组
			int len;
			while ((len = fr.read(b)) != -1) {
				fw.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fr != null)
					fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
代码语言:javascript
复制
	/**通过缓冲流实现文件的复制(可以调高文件复制的效率)
	 * 对于输出的缓冲流,写出的数据会先在内存中缓存,
	 * 使用flush()将会使内存中的数据立刻写出
	 */
	@Test
	public void testBufferedInputStreamOutputStream() {
		long start = System.currentTimeMillis();
		// 1.
		File file1 = new File("C:/Users/dell/Desktop/spider.rar");
		File file2 = new File("file/1.rar");
		// 2.
		FileInputStream fis = null;//输入流
		FileOutputStream fos = null;//输出流
		BufferedInputStream bis = null;//缓冲输入流
		BufferedOutputStream bos = null;//缓冲输出流
		try {
			fis = new FileInputStream(file1);//构造
			fos = new FileOutputStream(file2);

			// 3.
			bis = new BufferedInputStream(fis);//缓冲流的构造
			bos = new BufferedOutputStream(fos);
			
			byte[] b = new byte[1024];//字节数组,每次读取1024字节
			int len;
			while((len = bis.read(b)) != -1){
				bos.write(b, 0, len);
				bos.flush();//这句非常的重要,flush()将会使内存中的数据立刻写出
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(bos != null){
				try {
					bos.close();//关闭缓冲流时,会自动关闭它所包装的底层节点流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}			
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("spend time:" + (end - start));
	}

将上面的测试函数写成通用的函数:

代码语言:javascript
复制
	public void copyFile(String src, String des) {
		long start = System.currentTimeMillis();
		// 1.
		File file1 = new File(src);
		File file2 = new File(des);
		// 2.
		FileInputStream fis = null;//输入流
		FileOutputStream fos = null;//输出流
		BufferedInputStream bis = null;//缓冲输入流
		BufferedOutputStream bos = null;//缓冲输出流
		try {
			fis = new FileInputStream(file1);//构造
			fos = new FileOutputStream(file2);

			// 3.
			bis = new BufferedInputStream(fis);//缓冲流的构造
			bos = new BufferedOutputStream(fos);
			
			byte[] b = new byte[1024];//字节数组,每次读取1024字节
			int len;
			while((len = bis.read(b)) != -1){
				bos.write(b, 0, len);
				bos.flush();//这句非常的重要,flush()将会使内存中的数据立刻写出
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(bos != null){
				try {
					bos.close();//关闭缓冲流时,会自动关闭它所包装的底层节点流
				} catch (IOException e) {
					e.printStackTrace();
				}
			}			
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("spend time:" + (end - start));
	}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年03月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档