IO_四大抽象类及标准步骤[Java]


title: ‘IO_四大抽象类及标准步骤[Java]’
author: JF
Blog: https://www.huqifa.com/
summary: IO_四大抽象类
tags:

  • Java
    categories:
  • Java
    abbrlink: 14359
    date: 2020-04-14 23:23:27

1. IO_四大抽象类

抽象类说明常用方法
InputStream字节输出流的父类,数据单位为字节1. int read() 2.void close()
OutputStream字节输出流的父类,数据单位为字节1. void write(int) 2. void flush() 3. void close()
Reader字符输入流的父类,数据单位为字符1. int read() 2. void close()
Writer字符输出流的父类,数据单位为字符1. void write(String) 2. void flush() 3. void close()

具体方法,可以参考api文档

  • 字节流处理一切,字符只能处理文本

2. 标准步骤

package cn.io;

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

/**
 * 第一个程序:理解操作步骤
 * 1. 创建源
 * 2. 选择流
 * 3. 操作
 * 4. 释放资源
 * @author HQF
 *
 */
public class IoTest01 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//1. 创建源
		File src = new File("abc");
		
		//2. 选择流
		try {
			InputStream iStream = new FileInputStream(src);
		
			//3. 操作(读取)
			int data1 = iStream.read(); // 第一个数据
			int data2 = iStream.read();
			int data3 = iStream.read();
			
			//4. 释放资源
			System.out.println((char)data1);
			System.out.println((char)data2);
			System.out.println((char)data3);
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


package cn.io;

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

/**
 * 第一个程序:理解操作步骤
 * 1. 创建源
 * 2. 选择流
 * 3. 操作
 * 4. 释放资源
 * @author HQF
 *
 */
public class IoTest02 {

	public static void main(String[] args) throws IOException {
		//1. 创建源
		File src = new File("abc");
		InputStream iStream = null;
		//2. 选择流
		try {
			 iStream = new FileInputStream(src);
		
			//3. 操作(读取)
			int temp;
			while((temp = iStream.read()) != -1) {
				System.out.println((char)temp);
			}
				iStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally {
			iStream.close();
		}
	}

}


文件字节流 FileInputStream&FileOutputStream

  • FileInputStream:通过字节的方式读取文件,适合读取所有类型的文件(图像,视频等),全字符请考虑FileReader

  • FileOutputStream:通过字节的方式写出或追加数据到文件,适合所有类型的文件(图像,视频等),全字符请考虑FileWrite。

package cn.io;
//FileInputStream
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class IoTest03 {

	public static void main(String[] args) throws IOException {
		//1. 创建源
		File srcFile = new File("abc");
		//2. 选择流
		InputStream iStream = null;
		try {
			iStream = new FileInputStream(srcFile);
			//3. 操作(分段读取)
			byte[] flush = new byte[1024*50];//缓冲容器
			int len = -1;//接受长度
			while((len = iStream.read(flush)) != -1) {
				//字节数组-->字符串(解码)
				String string = new String(flush,0,len);
				System.out.println(string);
			}
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


文件输出流

package cn.io;

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

/**
 * 文件字节输出流
 * 1. 创建源
 * 2. 选择流
 * 3. 操作(写出内容)
 * 4. 释放资源
 * @author HQF
 *
 */
public class IoTest04 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//1. 创建源
		File srcFile = new File("ab.txt");
		//2. 选择流
		OutputStream oStream = null;
	
		
		try {
			oStream = new FileOutputStream(srcFile,true);
			//3. 操作
			String msgString = "IO is so easy";
			byte[] datas = msgString.getBytes();
			oStream.write(datas,0,datas.length);
			oStream.flush();
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (null != oStream) {
				oStream.close();
			}
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值