【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();
		}
	}
}

 

猜你喜欢

转载自blog.csdn.net/lovexiaotaozi/article/details/80998884
今日推荐