天天看点

Java中的IO流-字节输出流——FileOutputStream

Java中的IO流——FileOutputStream

1、IO流的分类

 * IO流的分类:

 *         流向:

 *             输入流 :读取数据

 *             输出流 :写出数据

 *         数据类型:

 *              字节流

 *                   字节输入流 :读取数据    InputStream

 *                   字节输出流 :写出数据    OutputStream

 *              字符流

 *                   字符输入流 :读取数据    Reader

 *                   字符输出流 :写出数据    Writer

 *

 *   注意:一般我们在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况下是按照数据类型来分的。

 *

Java中的IO流-字节输出流——FileOutputStream
Java中的IO流-字节输出流——FileOutputStream
Java中的IO流-字节输出流——FileOutputStream
Java中的IO流-字节输出流——FileOutputStream

2、 FileOutputStream案例:

需求:我要往一个文本文件中输入一句话:"hello,io"

 * 分析:

 *         A:这个操作最好是采用字符流来做,但是呢,字符流是在字节流之后才出现的,所以,先讲解字节流如何操作。

 *         B:由于是要往文件中写一句话,所以我们要采用字节输出流。

 *

 * 通过上面的分析后我们知道要使用:OutputStream

 * 但是通过查看API,我们发现该流对象是一个抽象类,不能实例化。

 * 所以,我们要找一个具体的子类。

 * 而我们要找的子类是什么名字的呢? 这个时候,很简单,我们回想一下,我们是不是要往文件中写东西。

 * 文件是哪个单词:File

 * 然后用的是字节输出流,联起来就是:FileOutputStream

 * 注意:每种基类的子类都是以父类名作为后缀名。

(四大基类:OutputStream、InputStream、Reader、Writer)

 *         XxxOutputStream

 *         XxxInputStream

 *         XxxReader

 *         XxxWriter

 * 查看FileOutputStream的构造方法:

 *         FileOutputStream(File file)

 *        FileOutputStream(String name)

 *

 * 字节输出流操作步骤:

 *         A:  创建字节输出流对象

 *         B:  写数据

 *         C:  释放资源

代码实现:

package cn.itcast_02;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		// 创建字节输出流对象
		// FileOutputStream(File file)
		// File file = new File("fos.txt");
		// FileOutputStream fos = new FileOutputStream(file);
		// FileOutputStream(String name)
		FileOutputStream fos = new FileOutputStream("fos.txt");
		/*
		 * 创建字节输出流对象了做了几件事情: 
		  A:调用系统功能去创建文件 
		  B:创建fos对象 C:把fos对象指向这个文件
		 */

		// 写数据
		fos.write("hello,IO".getBytes());
		fos.write("hello,Java".getBytes());

		// 释放资源
		// 关闭此文件输出流并释放与此流有关的所有系统资源。
		fos.close();
		/*
		 * 为什么一定要close()呢? 
		 * A:让流对象变成垃圾,这样就可以被垃圾回收器回收了 
		 * B:通知系统去释放跟该文件相关的资源
		 */
		// java.io.IOException: Stream Closed
		// fos.write("java".getBytes());
	}
}
           

注意:

        // 释放资源

        // 关闭此文件输出流并释放与此流有关的所有系统资源。

        fos.close();

         * 为什么一定要close()呢?

         * A: 让流对象变成垃圾,这样就可以被垃圾回收器回收了 。

         * B: 通知系统去释放跟该文件相关的资源。

3、FileOutputStream :字节输出流的操作步骤、write方法

 * 字节输出流操作步骤:

 * A:  创建字节输出流对象

 * B:  调用write( )方法

 * C:  释放资源

 * public void write(int b)  :写一个字节

 * public void write(byte[] b)  :写一个字节数组

 * public void write(byte[] b,int off,int len)  :写一个字节数组的一部分

package cn.itcast_01;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 创建字节输出流对象
		// OutputStream os = new FileOutputStream("fos2.txt"); // 多态
		FileOutputStream fos = new FileOutputStream("fos2.txt");

		// 调用write()方法
		// public void write(int b):写一个字节
		//fos.write(97); //97 -- 底层二进制数据	-- 通过记事本打开 -- 找97对应的字符值 -- a
		// fos.write(57);
		// fos.write(55);
		
		//public void write(byte[] b):写一个字节数组
		byte[] bys={97,98,99,100,101};
		fos.write(bys);
		
		//public void write(byte[] b,int off,int len):写一个字节数组的一部分
		fos.write(bys,1,3);
		
		//释放资源
		fos.close();
	}
}
           

4、加入异常处理的字节输出流操作:FileOutputStream

package cn.itcast_01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 加入异常处理的字节输出流操作
 */
public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 分开做异常处理
		// FileOutputStream fos = null;
		// try {
		// fos = new FileOutputStream("fos4.txt");
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.write("java".getBytes());
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 一起做异常处理
		// try {
		// FileOutputStream fos = new FileOutputStream("fos4.txt");
		// fos.write("java".getBytes());
		// fos.close();
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 改进版
		// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 为了保证close()一定会执行,就放到这里了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
           

开发版本**********************************

package cn.itcast_01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 加入异常处理的字节输出流操作
 */
public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 改进版
		// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值。
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 为了保证close()一定会执行,就放到这里了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}