InputStream类和OutputStream类

9 篇文章 0 订阅

InputStream类

package com.mipo.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * InputStream抽象类是表示字节输入流的所有类的超类,它以字节为单位从数据源中读取数据。
 * FileInputStream是InputStream的子类。
 * 文件流是指那些专门用于操作数据源中文件的流,主要有FileInputStream,FileOutputSteam,FileReader,FileWriter四个类。
 * 1个汉字字符=2 byte(字节)=16 bit(位)
 * 1个英文字符=1 byte(字节)=8 bit(位)
 * @author Administrator
 *
 */
public class TestInputStream {
	//在Unicode编码中,一个英文字符是用一个字节编码的,一个中文字符是用两个字节编码的,所以用字节流读取中文时会乱码。
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			//第一步:创建一个链接到指定文件的FileInputStream对象
			fis = new FileInputStream("D:\\Personal\\Desktop\\IO\\File\\demo\\readme.txt");//fis = new FileInputStream(File f)亦可
			System.out.println("可读取的字节数:"+fis.available());
			
			//第二步:读数据,一次读取一个字节的数据,返回的是读到的字节
			int i = fis.read();
			while (i != -1) {//若遇到流的末尾,会返回-1
				System.out.print((char)i);
				i = fis.read(); //再读
			}
//			byte[] b = new byte[1000];
//			int i = fis.read(b);//从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
//			for (byte d : b) {//foreach(元素类型   元素变量 : 对象集合或数组表达式)
//				System.out.println((char)d);
//			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//关闭输入流
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
OutputStream类

package com.mipo.file;

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

/**
 * OutputStream抽象类是表示字节输出流的所有类的超类,它以字节为单位向数据源写出数据
 * FileOutputStream类是OutputStream的子类
 * 一般来说,FileInputStream和FileOutputStream类用来操作二进制文件比较合适,如图片,声音,视频等
 * 
 * IO流操作文件流程:
 * 1,创建连接到指定数据源的IO流对象。
 * 2,利用IO流提供的方法进行数据的读取和写入。在整个操作过程中,都需要处理java.io.IOException异常。
 *   另外,如果是向输出流写入数据,在写入操作完成后,调用flush()方法强制写出所有缓冲的数据。
 * 3,操作完毕后,调用close()方法关闭该IO流对象。
 * @author Administrator
 *
 */
public class TestOutputStream {

	public static void main(String[] args) {
		FileOutputStream fos = null;
		try {
			//第一步:创建一个向指定文件名的文件中写入数据的FileOutputStream对象
			//第二个参数设置为true,表示使用追加模式添加字节
			fos = new FileOutputStream("D:\\Personal\\Desktop\\IO\\File\\demo\\readme.txt",true);
			//第二步:写数据
			fos.write('#');
			//用字符文件输出流往文件中写入的中文字符没有乱码,这是因为程序先把中文字符转成了字节数组然后再向文件中写
			//Windows操作系统的记事本程序在打开文本文件是能自动“认出”中文字符
			fos.write("hello,world!".getBytes());
			fos.write("你好".getBytes());
			//第三步:刷新输出流
			fos.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					//第四步:关闭输出流
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值