JAVA SE学习day_05: IO与流操作

一、IO与流

以程序为主体,input(输入)是读、获取、使用输入流的过程,output(输出)是写、发送的过程。

JAVA IO 标准的输入与输出

JAVA IO将读和写按照方向划分为:

  • 输入:用于读取
  • 输出:用于写出
  • 区分方向的参照点是程序,从外界获取信息到程序中的方向是输入,反之是输出。
  • JAVA IO以标准化的方式对外界设备进行读写操作。使得我们读写外界设备的方式
  • 基本是统一的。
输入流与输出流
  • 输入流:java.io.InputStream,它是所有字节输入流的超类,定义了所有字节输入 流都必须具备的读取数据的方法。
  • 输出流:java.io.OutputStream,所有字节输出流的超类,定义了写出操作。
  • 流的读写形式是顺序读写模式,即:读写数据只能顺序向后进行,是不能回退的。
节点流与处理流
  • 流除了按照读写方向分类外,还有一种分类就是:节点流与处理流
  • 节点流:数据源明确,是真实连接程序与另一端的"管道",负责实际读写数据的流读写一定是建立在节点流的基础上进行的。它在行业里也称为:低级流
  • 处理流:不能独立存在,必须连接在其他流上,目的是当数据流经当前流时,可以对数据进行加工处理,简化我们的操作,行业里也称为:高级流
文件流
  • java.io.FileInputStream和FileOutputStream
    文件流是一对低级流,用于读写文件的流。功能上与RandomAccessFile一致, 但是文件流是基于JAVA标准的IO,读写是分开的,并且流的读写是顺序读写, 只能顺序向后进行读或写操作,是不能回退的。
    但是RAF是基于指针的随机读写形式,可以调整指针位置对文件任意位置读写。

二、文件流(输出流,向程序内写入数据)

文件流的两种模式
* 1.覆盖模式
* FileOutputStream(String path)
* FileOutputStream(File file)
* 覆盖模式:在写下构造方法时,如果指定的文件中存在数据 则删除当前文件中的所有数据后进行操作
*
* 2.追加模式,对应的构造方法:
* FileOutputStream(String path,boolean append)
* FileOutputStream(File file,boolean append)
* 追加模式:当第二个参数为true,创建文件流时,如果指定文件 已经存在,则通过追加当前流写入的内容都会被顺序追加到文件末尾

//1 FileOutputStream fos = new FileOutputStream("./fos.txt");
FileOutputStream fos = new FileOutputStream("./fos.txt",true);
//2
//3File file = new File("./fos.txt");
//FileOutputStream fos = new FileOutputStream(file);
		/*1and3 String ling = "asdasdasda123@#";
		byte[]data = ling.getBytes("utf-8");
		fos.write(data);*/
		String ling = "asdasdasda123@#";
		byte[]data = ling.getBytes("utf-8");
		fos.write(data);
		//2
		System.out.println("over");
		fos.close();

三、输入流(从文件中读取数据)

文件输入流,用于从文件中读取字节

FileInputStream fis = new FileInputStream("./fos.txt");
		
		byte[]data = new byte[200];
		int len = fis.read(data);
		System.out.println("实际读取到了"+len+"个字节");
		String str = new String(data,0,len,"utf-8");
		System.out.println(str);
		fis.close();

四、缓存流(属于处理流)

java.io.BufferedInputStream和BufferedOutputstream

  • 缓冲流是一种高级流,在流的连接中作用为加快读写效率。无论我们使用块读写还是单字节读写,缓冲流最终都会统一转换为块读写以保证 读写效率
package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyDemo2 {

	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("image.png");
		BufferedInputStream bis = new BufferedInputStream(fis);
		FileOutputStream fos = new FileOutputStream("image77.png");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		int d = 0;
		long start = System.currentTimeMillis();
		while((d=bis.read())!=-1){
			bos.write(d);
		}
		long end = System.currentTimeMillis();
		System.out.println("copy over!usetime:"+(end-start));
		bis.close();
		bos.close();
	}

}

缓冲字节输出流写数据的缓冲区问题

FileOutputStream fos = new FileOutputStream("raf.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		String str = "1231231231asdasddas";
		byte[]data = str.getBytes("utf-8");
		bos.write(data);
		/*缓冲流在没有装够8kb数据时不运行,这个时候就需要我们引用
		 * void flush()
		 * 强制将缓冲流的缓冲区中已经缓存的数据写出
		 */
		bos.flush();
		System.out.println("over");
		bos.close();

五、对象流

java.io.ObjectInputStream和ObjectOutputStream
对象流是一对高级流,作用是方便读写任何java对象

package io;

import java.io.Serializable;
import java.util.Arrays;

/**
 * 使用当前类测试对象流的对象读写操作
 * @author 毛
 *
 */
public class Person implements Serializable{
/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1L;
private String name;
private int age;
private String gender;
private transient String[]otherInfo;
/*
 * transient关键字
 * 当一个属性被修饰后,那么当对象在序列化的过程中,这个属性会被忽略
 * 忽略不重要的属性可以达到减少内存的占用效果
 */

public Person(String name, int age, String gender, String[] otherInfo) {
	super();
	this.name = name;
	this.age = age;
	this.gender = gender;
	this.otherInfo = otherInfo;
}
	public String toString(){
		return name+","+age+","+gender+","+Arrays.toString(otherInfo);
	}

}
5.1 实例写入操作(序列化)

ObjectOutputStream用于对象的序列化
ObjectOutputStream提供了writeObject方法
writeObject方法要求写出的对象所属的类必须实现接口:

  • Serializable _序列化接口 (签名接口,两个中的其一)
  • 否则会抛出异常

在流连接中,对象流的工作是:对象序列化,就是将给定的对象按照其结构转换为一组字节的过程。
然后这组字节再经过文件流写入文件,这个过程称为数据持久化,因为写入文件就是写入磁盘,断电也可保存;

package io;

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

public class OOSDemo {

	public static void main(String[] args) throws IOException {
		/*
		 * 将一个Person实例写入文件person.obj
		 */
		String name = "苍老师";
		int age = 18;
		String gender="nv";
		String[]otherInfo = {"是个演员","来自日本"};
		Person p = new Person(name,age,gender,otherInfo);
		System.out.println(p);
		
		FileOutputStream fos = new FileOutputStream("person.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		oos.writeObject(p);
		System.out.println("over");
		oos.close();
	}

}
5.2 反序列化

ObjectInputStream用于对象的反序列化
ObjectInputStream提供的方法

  • Object readObject()
  • 该方法用于进行对象的反序列化,如果读取的字节不是一个 对象的时候(字节不是通过对象输出流序列化的),会抛异常;
package io;

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

public class OISDemo {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		FileInputStream fis = new FileInputStream("person.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);

		Person p = (Person) ois.readObject();
		System.out.println(p);
		ois.close();
	}

}
练习
/*5 FileOutputStream fos1 = new FileOutputStream("./fos.txt");
		String a = "45456456";
		byte[]date = a.getBytes("utf-8");
		fos1.write(date);
		System.out.println("over");
		fos1.close();*/
		/*4FileOutputStream fos1 = new FileOutputStream("./fos1.txt");
		String a = "wqeqwwe";
		byte[]date = a.getBytes();
		fos1.write(date);
		System.out.println("over");
		fos.close();
		*//*3
		FileOutputStream fos2 = new FileOutputStream("./fos2.txt");
		String s="asdas";
		byte[]date1 = s.getBytes();
		fos2.write(date1);
		System.out.println("over");
		fos2.close();
		*/
		/*2FileOutputStream fos3 = new FileOutputStream("./fos3.txt");
		String as = "asdasdasd";
		byte[]date = as.getBytes();
		fos3.write(date);
		System.out.println("over");
		fos3.close();*/
		/*1
		FileOutputStream fos4 = new FileOutputStream("./fos4.txt");
		String asd = "!@#!@#!@#";
		byte[]date1=asd.getBytes();
		fos4.write(date1);
		System.out.println("over");
		fos4.close();*/
/*5
		FileInputStream fis1 = new FileInputStream("./fos1.txt");
		byte[]date = new byte[200];
		int l = fis1.read(date);
		System.out.println("实际读取到了"+l+"个字节");
		String s = new String(date,0,l,"utf-8");
		System.out.println(s);
		fis1.close();
		*/
		/*4FileInputStream fis2 = new FileInputStream("./fos2.txt");
		byte[]date = new byte[300];
		int l = fis2.read(date);
		System.out.println("实际读取到了"+l+"个字节");
		String sa = new String(date,0,l,"utf-8");
		System.out.println(sa);
		fis2.close();
		*/
		/*3FileInputStream fis3 = new FileInputStream("./fos3.txt");
		byte[]date = new byte[400];
		int l=fis3.read(date);
		System.out.println("实际读取到了"+l+"个字节");
		String saa  = new String(date,0,l,"utf-8");
		System.out.println(saa);
		fis3.close();
		*/
		/*2FileInputStream fis4 = new FileInputStream("./fos4.txt");
		byte[]date = new byte[200];
		int l = fis4.read(date);
		System.out.println("实际读取到了"+l+"个字节");
		String sa = new String(date,0,l,"utf-8");
		System.out.println(sa);
		fis4.close();
		/*
		/*1FileInputStream fis5 = new FileInputStream("./fos5.txt");
		byte[]date = new byte[200];
		int l = fis5.read(date);
		System.out.println("实际读取到了"+l+"个字节");
		String as = new String(date,0,l,"utf-8");
		System.out.println(as);
		fis5.close();*/
		/*5FileInputStream fis1 = new FileInputStream("image.png");
		BufferedInputStream bis1 = new BufferedInputStream(fis1);
		FileOutputStream fos1 = new FileOutputStream("image1.png");
		BufferedOutputStream bos1 = new BufferedOutputStream(fos1);
		int a=0;
		while((a=bis.read())!=-1){
			bos1.write(a);
		}
		System.out.println("over");
		bis1.close();
		bos1.close();
		*/
		/*4 FileInputStream fis2 = new FileInputStream("image.png");
		FileOutputStream fos2 = new FileOutputStream("image2.png");
		BufferedInputStream bis2 = new BufferedInputStream(fis2);
		BufferedOutputStream bos2 = new BufferedOutputStream(fos2);
		int b = 0;
		while((b=bis2.read())!=-1){
			bos2.write(b);
		}
		System.out.println("over");
		bis2.close();
		bos2.close();
		*/
		/*3FileInputStream fis3 = new FileInputStream("image.png");
		FileOutputStream fos3 = new FileOutputStream("image3.png");
		BufferedInputStream bis3 = new BufferedInputStream(fis3);
		BufferedOutputStream bos3 = new BufferedOutputStream(fos3);
		int s = 0;
		while((s = bis3.read())!=-1){
			bos3.write(s);
		}
		System.out.println("over");
		bis3.close();
		bos3.close();
		*/
		/*2FileInputStream fis4 = new FileInputStream("ias.mp4");
		FileOutputStream fos4 = new FileOutputStream("ias1.mp4");
		BufferedInputStream bis4 = new BufferedInputStream(fis4);
		BufferedOutputStream bos4 = new BufferedOutputStream(fos4);
		int as = 0;
		while((as = bis4.read())!=-1){
			bos4.write(as);
		}
		System.out.println("over");
		bis4.close();
		bos4.close();
		*/
		/*1FileInputStream fis0 = new FileInputStream("123.dat");
		FileOutputStream fos0 = new FileOutputStream("1234.dat");
		BufferedInputStream bis0 = new BufferedInputStream(fis0);
		BufferedOutputStream bos0 = new BufferedOutputStream(fos0);
		int dd = 0;
		while((dd = bis0.read())!=-1){
			bos0.write(dd);
		}
		System.out.println("over");
		bis0.close();
		bos0.close();
		*/
/*5FileOutputStream fos1 = new FileOutputStream("raf.txt");
		BufferedOutputStream bos1 = new BufferedOutputStream(fos1);
		String s = "sadasdsad";
		byte[]date = s.getBytes("utf-8");
		bos1.write(date);
		bos1.flush();
		System.out.println("over");
		bos1.close();
		*/
		/*4FileOutputStream fos2 = new FileOutputStream("raf1.txt");
		BufferedOutputStream bos2 = new BufferedOutputStream(fos2);
		String sa = "!@#$@#$";
		byte[]data1 = sa.getBytes("utf-8");
		bos2.write(data1);
		bos2.flush();
		System.out.println("over");
		bos2.close();
		*/
		/*3FileOutputStream fos3 = new FileOutputStream("raf2.txt");
		BufferedOutputStream bos3 = new BufferedOutputStream(fos3);
		String as = "dsasdasdas";
		byte[]date = as.getBytes("utf-8");
		bos3.write(date);
		bos3.flush();
		System.out.println("over");
		bos3.close();
		*/
		/*2FileOutputStream fos4 = new FileOutputStream("raf3.txt");
		BufferedOutputStream bos4 = new BufferedOutputStream(fos4);
		String sa = "asddsa231123";
		byte[]data2 = sa.getBytes("utf-8");
		bos4.write(data2);
		bos4.flush();
		System.out.println("over");
		bos4.close();
		*/
		/*1FileOutputStream fos0 = new FileOutputStream("raf0.txt");
		BufferedOutputStream bos5 = new BufferedOutputStream(fos0);
		String ds = "asasdasd";
		byte[]date1 = ds.getBytes("utf-8");
		bos5.write(date1);
		bos5.flush();
		System.out.println("over");
		bos5.close();*/
package io;

import java.io.Serializable;
import java.util.Arrays;

/**
 * 使用当前类测试对象流的对象读写操作
 * @author 毛
 *
 */
public class Person implements Serializable{
/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1L;
private String name;
private int age;
private String gender;
private transient String[]otherInfo;
/*
 * transient关键字
 * 当一个属性被修饰后,那么当对象在序列化的过程中,这个属性会被忽略
 * 忽略不重要的属性可以达到减少内存的占用效果
 */

public Person(String name, int age, String gender, String[] otherInfo) {
	super();
	this.name = name;
	this.age = age;
	this.gender = gender;
	this.otherInfo = otherInfo;
}
	public String toString(){
		return name+","+age+","+gender+","+Arrays.toString(otherInfo);
	}

}

		/*5String name = "苍老师";
		int age = 18;
		String gender="nv";
		String[]otherInfo = {"是个演员","来自日本"};
		Person p = new Person(name,age,gender,otherInfo);
		FileOutputStream fos = new FileOutputStream("person.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(p);
		System.out.println("over");
		oos.close();*/
		/*4String name1 = "苍老师";
		int age1 = 18;
		String gender1="nv";
		String[]otherInfo1 = {"是个演员","来自日本"};
		Person p1 = new Person(name1,age1,gender1,otherInfo1);
		FileOutputStream fos1 = new FileOutputStream("person.dat");
		ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
		oos1.writeObject(p1);
		System.out.println("over");
		oos1.close();*/
		/*3
		String name2 = "苍老师";
		int age2 = 18;
		String gender2="nv";
		String[]otherInfo2 = {"是个演员","来自日本"};
		Person p2 = new Person(name2,age2,gender2,otherInfo2);
		FileOutputStream fos2 = new FileOutputStream("person.dat");
		ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
		oos2.writeObject(p2);
		System.out.println("over");
		oos2.close();
		*/
		/*2String name3 = "苍老师";
		int age3 = 18;
		String gender3="nv";
		String[]otherInfo3 = {"是个演员","来自日本"};
		Person p3 = new Person(name3,age3,gender3,otherInfo3);
		FileOutputStream fos3 = new FileOutputStream("person.dat");
		ObjectOutputStream oos3 = new ObjectOutputStream(fos3);
		oos3.writeObject(p3);
		System.out.println("over");
		oos3.close();
		*/
		/*1String name4 = "苍老师";
		int age4 = 18;
		String gender4="nv";
		String[]otherInfo4 = {"是个演员","来自日本"};
		Person p4 = new Person(name4,age4,gender4,otherInfo4);
		FileOutputStream fos4 = new FileOutputStream("person.dat");
		ObjectOutputStream oos4 = new ObjectOutputStream(fos4);
		oos4.writeObject(p4);
		System.out.println("over");
		oos4.close();*/
/*5
		FileInputStream fis1 = new FileInputStream("person.dat");
		ObjectInputStream ois1 = new ObjectInputStream(fis1);
		Person p1 = (Person)ois1.readObject();
		System.out.println(p1);
		ois1.close();*/
		/*4
		FileInputStream fis2 = new FileInputStream("person.dat");
		ObjectInputStream ois2 = new ObjectInputStream(fis2);
		Person p2 = (Person)ois2.readObject();
		System.out.println(p2);
		ois2.close();*/
		/*3
		FileInputStream fis3 = new FileInputStream("person.dat");
		ObjectInputStream ois3 = new ObjectInputStream(fis3);
		Person p3 = (Person)ois3.readObject();
		System.out.println(p3);
		ois3.close();*/
		/*2
		FileInputStream fis4 = new FileInputStream("person.dat");
		ObjectInputStream ois4 = new ObjectInputStream(fis4);
		Person p4 = (Person)ois4.readObject();
		System.out.println(p4);
		ois4.close();*/
		/*1
		FileInputStream fis0 = new FileInputStream("person.dat");
		ObjectInputStream ois0 = new ObjectInputStream(fis0);
		Person p0 = (Person)ois0.readObject();
		System.out.println(p0);
		ois0.close();*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值