Java的IO流 - FileInputStream - FileOutputStream -(包含图片或视频的复制)

在大二的时候草草的学了一些,那时候很懵只是有个大概印象,如今来总结一下

流的分类:
 * 按照数据流向的不同:输入流  输出流
 * 按照处理数据的单位的不同:字节流(8bit)  字符流(处理的文本文件 16bit)
 * 按照角色的不同:节点流(直接作用于文件的)  处理流

所有文件的操作类都是在IO包下的,基类为如下4个抽象基类:

 来放下他们的大家族的合照。。。:

嗯。。。有点多,稳住别怕。。。主要使用的是上图蓝色底框的类。


个人学习时候的疑问:

什么是 输入流  输出流:

内存中写人是输入流、向外存中输出是输出流(很好记,冯诺依曼体系嘛。。内存为中心)

什么是字节流、字符流

个人理解就是这个流的粗细,字节流8bit传的宽度少,字节流16bit宽度大

什么是节点流、 处理流

如下图:

个人理解:节点流相当于电线里的铜丝(地位很重要的那种),处理流相当于包裹电线的外壳(还可以给电线增加一些功能)

那个中间的管道想象成一个解刨面

 


FileInputStream使用示例:(注意那个byte[]数组,是为了一次读取多个数据到byte[]数组里,就像是送水的不是一个一个抗来的,而是拖了一个小板车一次就搬来了好多桶水。我以前学Android的时候理解错了。。。注意下)

write(buff, 0, cbRead);意思:从buff中读取0~cbRead数据存入OutputStream

@Test
	public void testFileInputStream3() { // abcdefgcde
		FileInputStream fis = null;
		try {
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			byte[] b = new byte[5];// 读取到的数据要写入的数组。
			int len;// 每次读入到byte中的字节的长度
			while ((len = fis.read(b)) != -1) {
				// for (int i = 0; i < len; i++) {
				// System.out.print((char) b[i]);
				// }
				String str = new String(b, 0, len);
				System.out.print(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
	}

下方这一段看看就好(没读到byte[]里)

	@Test
	public void testFileInputStream2() {
		// 2.创建一个FileInputStream类的对象
		FileInputStream fis = null;
		try {
			// 1.创建一个File类的对象。
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			// 3.调用FileInputStream的方法,实现file文件的读取。
			int b;
			while ((b = fis.read()) != -1) {
				System.out.print((char) b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 4.关闭相应的流
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

FileOutputStream使用示例:

学到这想起了以前做过的一个项目,python为服务端和java为客户端通过socket传输图片,当时可是憋炸了(主要还是Java语法不熟悉憋了好长时间才做出来。。。唉。。。)

先看下基本的使用:

// FileOutputStream
	@Test
	public void testFileOutputStream() {
		// 1.创建一个File对象,表明要写入的文件位置。
		// 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
		File file = new File("hello2.txt");
		// 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			// 3.写入的操作
			fos.write(new String("I love China!").getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 4.关闭输出流
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

下方是文件的拷贝,图片视频都可以,封装在了copyFile方法里,记录下以后直接用就好。。

	@Test
	public void testCopyFile(){
		long start = System.currentTimeMillis();
//		String src = "C:\\Users\\shkstart\\Desktop\\1.avi";
//		String dest = "C:\\Users\\shkstart\\Desktop\\2.avi";
		String src = "dbcp.txt";
		String dest = "dbcp2.txt";
		copyFile(src,dest);
		long end = System.currentTimeMillis();
		System.out.println("花费的时间为:" + (end - start));//3198
	}
	
	// 实现文件复制的方法
	public void copyFile(String src, String dest) {
		// 1.提供读入、写出的文件
		File file1 = new File(src);
		File file2 = new File(dest);
		// 2.提供相应的流
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			// 3.实现文件的复制
			byte[] b = new byte[1024];
			int len;
			while ((len = fis.read(b)) != -1) {
				// fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
				fos.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值