JAVA基础(五):基本的I/O流

介绍

在Java程序中,对于数据的输入/输出操作以“流” (stream) 方式进行,I/O是实现输入输出的基础。将磁盘文件、网络连接等读入程序称为输入流。将内容写出到文件、磁盘等称为输出流。

流分类

方向分类
输入流:从数据源读入程序(InputStream、Reader)
输出流:从程序读出(OutputStream、Writer)

作用分类
节点流:从数据源或目的地读写数据
处理流:对节点流进行封装,提高操作和性能
区别:
1、节点流处于io操作的基础,所有流操作都要用节点流
2、处理流可以对其他流处理(提高效率)、几乎都是通过装饰者模式实现
常见的处理流:
1、缓冲流:在读入或写出时,对数据进行缓存,以减少I/O的次数:BufferedReader与BufferedWriter,BufferedInputStream和BufferedOutputStream
2、对象流:可以对实例对象进行读写操作。ObjectInputStream和ObjectOutputStream
3、数据流:按基本类型和String类型读写。DataInputStream和DataOutputStream

数据分类
字节流:按字节读取数据(InputStream、OutputStream),以InputStream和OutputStream作为基类
字符流:按字符读取数据(Reader、Writer),以Reader和Writer作为基类,专门对字符流处理可以提高效率。原理还是通过字节流的操作,封装的方法实现了对指定编码表的搜索

在这里插入图片描述
FileReader和FileWriter只能对纯文本文件操作,要操作任意类型的文件需要使用FileInputStream和FileOutputStream

I/O的操作

基类
InputStream
InputStream+Reader是所有输入流的抽象类,不能实例化,具有所有输入流可使用的方法,全字符请考虑FileReader
int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转为int型)
int read(byte []):从输入流中最多读取b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节数。
int read(byte[], int off, int len): 从输入流中最多读取len个字节的数据,并将其存储在数组b中,放入b数组时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数
Reader
int read():从输入流中读取单个字符,返回所读取的字符数据(可直接转为int类型)
int read(char[]):从输入流中最多读取b.length个字符的数据,并将其存储在数组b中,返回实际读取的字符数
int read(char[] b, int off, int len):从输入流中最多读取len个字符的数据,并将其存储在数组b中,放入数组b时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字符数。
OutputStream
输出流跟输入流操作基本一致
void write(int c):将指定的字节/字符输出到输出流中,c可以代表字节,也可以代表字符
void write(byte[]/byte[] buf):将字节数组,字符数组的数据输出到指定输出流中
void write(byte[]/byte[] buf, int off, int len):将字节数组/字符数组从off位置开始,长度为len的字节/字符数组输出到输出流中
Writer
void write(String str):将str字符串里包含的字符输出到指定输出流中
void write(String str, int off, int len):将str字符串里从off位置开始,长度为len的字符输出到指定输出流中
FileInputSteam&OutputSteam
FileInputStream:通过字节的 方式读取文件,适合读取所有 类型的文件(图像、视频等), 全字符请考虑FileReader
FileOutputStream:通过字节的 方式写出或追加数据到文件, 适合所有类型的文件(图像、视 频等),全字符请考虑 FileWriter

具体的使用步骤:
1、创建源
2、选择流
3、对流操作
4、释放资源

public static void main(String[] args) {
		//1、创建源
		File src = new File("a.txt");
		//2、选择流
		InputStream  is =null;
		try {
			is =new FileInputStream(src);
			//3、操作 (读取)
			int temp ;
			//read每次只返回一个字节
			while((temp=is.read())!=-1) {
				System.out.println((char)temp);
			}
			//3、操作 (分段读取)
			//byte[] flush = new byte[1024*10]; //缓冲容器
			//int len = -1; //接收长度
			//while((len=is.read(flush))!=-1) {
				//字节数组-->字符串 (解码)
				//String str = new String(flush,0,len);
				//System.out.println(str);
			}				
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

普通的输出流需要flush一下

//1、创建源
		File dest = new File("dest.txt");
		//2、选择流
		OutputStream os =null;
		try {
			os = new FileOutputStream(dest,true);
			//3、操作(写出)
			String msg ="Hello World\r\n";
			byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
			os.write(datas,0,datas.length);
			os.flush();
		}catch(FileNotFoundException e) {		
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4、释放资源
			try {
				if (null != os) {
					os.close();
				} 
			} catch (Exception e) {
			}
		}
	}

ByteArrayInputStream
字节数组输入流:在内存中有一个字节数组的缓冲区,可以把输入的字节数组保存在缓冲区中
创建方式:
1、字节数组传参

ByteArrayInputStream bArray = new ByteArrayInputStream(byte [] a);

2、一个字节数组,和两个整形变量 off、len,off表示第一个读取的字节,len表示读取字节的长度。

ByteArrayInputStream bArray = new ByteArrayInputStream(byte []a,                                                       int off,                                                       int len)

ByteArrayOutputStream:
1、创建源:内部维护
2、选择流:不需要跟数据源关联
3、操作(getBytes()/toByteArray())
4、可以不同释放

图片读入和写出demo

public static void main(String[] args) {
		//图片转成字节数组
		byte[] datas = fileToByteArray("a.jpg");
		System.out.println(datas.length);
		byteArrayToFile(datas,"copy.jpg");		
	}
	/**
	 * 1、图片读取到字节数组
	 * 1)、图片到程序  FileInputStream
	 * 2)、程序到字节数组	ByteArrayOutputStream
	 */
	public static byte[] fileToByteArray(String filePath) {
		//1、创建源与目的地
		File src = new File(filePath);
		byte[] dest =null;
		//2、选择流
		InputStream  is =null;
		ByteArrayOutputStream baos =null;
		try {
			is =new FileInputStream(src);
			baos = new ByteArrayOutputStream();
			//3、操作 (分段读取)
			byte[] flush = new byte[1024*10]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				baos.write(flush,0,len);		 //写出到字节数组中			
			}		
			baos.flush();
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;		
	}
	/**
	 * 2、字节数组写出到图片
	 * 1)、字节数组到程序 ByteArrayInputStream
	 * 2)、程序到文件 FileOutputStream
	 */
	public static void byteArrayToFile(byte[] src,String filePath) {
		//1、创建源
		File dest = new File(filePath);
		//2、选择流
		InputStream  is =null;
		OutputStream os =null;
		try {
			is =new ByteArrayInputStream(src);
			os = new FileOutputStream(dest);
			//3、操作 (分段读取)
			byte[] flush = new byte[5]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				os.write(flush,0,len);			//写出到文件	
			}		
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if (null != os) {
					os.close();
				} 
			} catch (Exception e) {
			}
		}
	}

I/O中的装饰

BufferedInputStream&BufferedOutputStream
在这里插入图片描述BufferedReader&BufferedWriter
在这里插入图片描述图片源于网上,侵权删

装饰就是在原有的结点流对象外部包装缓冲流,为读写操作增加缓冲区
目的:
缓冲流要套接在节点流之上,对读写的数据提供了缓冲功能,增加了读写的效率,同时增加了一些新的方法。例如:BufferedReader中的readLine方法,BufferedWriter中的newLine方法

Demo

public static void main(String[] args) {
		File src = new File("a.txt");
		//2、选择流
		InputStream  is =null;
		try {
			is =new BufferedInputStream(new FileInputStream(src));
			//3、操作 (分段读取)
			byte[] flush = new byte[1024]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				//字节数组-->字符串 (解码)
				String str = new String(flush,0,len);
				System.out.println(str);
			}		
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}
	}

ObjectInputStream&ObjectOutputStream

解决输入输出的数据不是基本数据类型,也不是字符型或字节型,而是创建的对象,对于这种情况,就可以使用处理流中的对象流。该对象必须实现序列化。

注意:
1、先序列化后反序列化; 反序列化顺序必须与序列化一致
2、不是所有对象都可以序列化 必须实现 java.io.Serializable
3、不是所有属性都需要序列化 不需要序列化的属性 要加 transient

序列化与反序列化:
Java 中对象的序列化就是将对象转换成二进制序列,反序列化则是将二进制序列转换成对象。
采用Java序列化与反序列化技术:
1、是可以实现数据的持久化。
2、是可以对象数据的远程通信。

demo

public class Student implements Serializable{
	private String name;
	private int age;
	private transient String id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * @param name
	 * @param age
	 * @param id
	 */
	public Student(String name, int age, String id) {
		super();
		this.name = name;
		this.age = age;
		this.id = id;
	}
	/**
	 * 
	 */
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
}

@Test
	void test() {
		try {
			//序列化
			ObjectOutputStream oos = new ObjectOutputStream(
					new BufferedOutputStream(new FileOutputStream(
							new File("d://b.txt")
							)));
			Student stu = new Student("atcain",20,"10001");
			oos.writeObject(stu);
			oos.close();
			//反序列化
			ObjectInputStream ois = new ObjectInputStream(
					new BufferedInputStream(
							new FileInputStream(
									new File("d://b.txt")
									)));
			System.out.println(ois.readObject());//Student [name=atcain, age=20]
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

序列化后的文件内容
在这里插入图片描述RandomAccessFile
随机流,用来访问那些保存数据记录的文件的,可以用seek( )方法来访问记录,并进行读写了。没怎么用到过,可以实现将文件分割

DataOutputStream&DataInputStream
数据流,写出后可以读取,并且读的顺序和写的顺序保持一致,常用于写基本数据类型

public static void main(String[] args) throws IOException {
		//写出
		ByteArrayOutputStream baos =new ByteArrayOutputStream();
		DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(baos));
		//操作数据类型 +数据
		dos.writeUTF("hello I/O");
		dos.writeInt(18);
		dos.writeBoolean(false);
		dos.writeChar('a');
		dos.flush();
		byte[] datas =baos.toByteArray();
		System.out.println(datas.length);
		//读取
		DataInputStream dis =new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
		//顺序与写出一致
		String msg = dis.readUTF(); 
		int age = dis.readInt();
		boolean flag = dis.readBoolean();
		char ch = dis.readChar();
		System.out.println(flag);
	}

常用的I/O基本在这里了,后面遇到新的操作方法再完善该文章

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值