【java基础】IO-Part2-流

1.流的分类和操作模板

 

 输入流输出流
字节流字节输入流(InputStream)字节输出流(OutputStream
字符流字符输入流(Reader)字符输出流(Writer)

1.1操作IO流的模板:

1.):创建源或者目标对象(挖井)

拿文件流举例:

输入操作:把文件中的数据流向到程序中,此时文件是源,程序是目标.

输出操作:把程序中的数据流向到文件中,此时文件是目标,程序是源.

2.):创建IO流对象(水管)

输入操作:创建输入流对象

输出操作:创建输出流对象

3.):具体的IO操作

 

输入操作:输入流对象的read方法.

输出操作:输出流对象的wirte方法.

4.):关闭资源(勿忘).

输入操作:输入流对象.close();

输出操作:输出流对象.close();

1.2操作IO流的六字箴言:

读进来,写出去(读进来强调的是输入,读说明是read方法,写出去强调的是输出,写说明是write方法.

2.文件流:顾名思义,程序和文件打交道.

 

 输入流输出流
字节流文件的字节输入流(FileInputStream)文件的字节输出流(FileOutputStream)
字符流文件的字符输入流(FileReader)文件的字符输出流(FileWriter)

3.通过文件的字节输入流和输出流实现对文件的拷贝,贴个小DEMO:

public static void main(String[] args) throws Exception {
		//1.创建源/目的地(水井)
		File srcFile = new File("file/srcFile.txt");
		File destFile = new File("file/destFile.txt");
		//2.创建流对象(管道)
		InputStream in = new FileInputStream(srcFile);
		OutputStream out = new FileOutputStream(destFile);
		//3.具体的IO操作:先把文件从源读入程序,再从程序写出到目的地.
		byte[] buffer = new byte[3];//这里因为我的文件比较小,如果是大文件默认为1024.
		int len = -1;//读了多少个字节,可以通过while循环来实现读完文件中所有内容,读到文件中无数据时,len为-1.
		while((len = in.read(buffer)) != -1) { //这里可以写 !=-1,也可以写>0,建议写-1,加深印象.
			System.out.println(new String(buffer,0,len));
			out.write(buffer, 0, len);
		}
		//4.关闭资源
		in.close();
	}

4.优雅地关闭资源.如果像如上代码直接抛异常的话,前面如果代码出错了,该资源就不会被关闭,造成浪费,因此需要优雅地关闭资源下面提供2种方式,第一种比较传统,代码看着较为臃肿,不优雅,之所以贴上来是因为面试的时候可能会碰到,因为这里有三个坑给大家一起分享一下:

public class FileCloseDemo {

	/**
	 * 演示如何优雅的关闭资源
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		test1();
		test2();
	}

	/**
	 * 不优雅的关闭资源
	 * 坑1:在finally中关闭资源时,需要把InputStream/OutputStream 定义成全局的变量.
	 * 坑2:close本身也要try catch,当有两个以上的资源需要关闭时,要分别对其try cache,如果放在一起的话,第一个关闭出错了,之后的就无法关闭.
	 *坑3: 在关闭前应该先判断资源是否为空,因为如果前面的代码就出错了,资源还未被创建,就会报空指针异常,因此需要先判断是否为Null.
	 */
	public static void test1() {
		InputStream in = null;
		OutputStream out = null;
		try {
			// 1.创建源/目的地(水井)
			File srcFile = new File("file/srcFile.txt");
			File destFile = new File("file/destFile.txt");
			// 2.创建流对象(管道)
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);
			// 3.具体的IO操作
			byte[] buffer = new byte[3];
			int len = -1;
			while ((len = in.read(buffer)) != -1) {
				System.out.println(new String(buffer, 0, len));
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 4.关闭资源
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 十分优雅的关闭资源,java7新特性,自动关闭资源,底层实现了自动关闭资源的接口
	 * 把打开资源的代码块放入try()内,注意是圆括号.
	 */
	public static void test2() {
		// 1.创建源/目的地(水井)
		File srcFile = new File("file/srcFile.txt");
		File destFile = new File("file/destFile.txt");
		try(// 2.创建流对象(管道)
				InputStream in = new FileInputStream(srcFile);
				OutputStream out = new FileOutputStream(destFile);
		){
			// 3.具体的IO操作
		  byte[] buffer = new byte[3];
		  int len = -1;
		  while ((len = in.read(buffer)) != -1) {
		  System.out.println(new String(buffer, 0, len));
		  out.write(buffer, 0, len);
		  }
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
	 * 不优雅的关闭资源
	 * 坑1:在finally中关闭资源时,需要把InputStream/OutputStream 定义成全局的变量.
	 * 坑2:close本身也要try catch,当有两个以上的资源需要关闭时,要分别对其try cache,如果放在一起的话,第一个关闭出错了,之后的就无法关闭.
	 *坑3: 在关闭前应该先判断资源是否为空,因为如果前面的代码就出错了,资源还未被创建,就会报空指针异常,因此需要先判断是否为Null.
	 */
	public static void test1() {
		InputStream in = null;
		OutputStream out = null;
		try {
			// 1.创建源/目的地(水井)
			File srcFile = new File("file/srcFile.txt");
			File destFile = new File("file/destFile.txt");
			// 2.创建流对象(管道)
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);
			// 3.具体的IO操作
			byte[] buffer = new byte[3];
			int len = -1;
			while ((len = in.read(buffer)) != -1) {
				System.out.println(new String(buffer, 0, len));
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 4.关闭资源
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 十分优雅的关闭资源,java7新特性,自动关闭资源,底层实现了自动关闭资源的接口
	 * 把打开资源的代码块放入try()内,注意是圆括号.
	 */
	public static void test2() {
		// 1.创建源/目的地(水井)
		File srcFile = new File("file/srcFile.txt");
		File destFile = new File("file/destFile.txt");
		try(// 2.创建流对象(管道)
				InputStream in = new FileInputStream(srcFile);
				OutputStream out = new FileOutputStream(destFile);
		){
			// 3.具体的IO操作
		  byte[] buffer = new byte[3];
		  int len = -1;
		  while ((len = in.read(buffer)) != -1) {
		  System.out.println(new String(buffer, 0, len));
		  out.write(buffer, 0, len);
		  }
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值