序列流、对象流、打印流和转换流

序列流、对象流、打印流和转换流

一、序列流

使用SequenceInputStream进行文件的合并:

public class Demo1 {
	
	public static void main(String[] args) throws IOException {
		merge3();
	}
	
	//把三个文件合并成一个文件
	public static void merge3() throws IOException{
		//找到目标文件
		File file1 = new File("F:\\a.txt");
		File file2 = new File("F:\\b.txt");
		File file3 = new File("F:\\c.txt");
		File file4 = new File("F:\\d.txt");
		
		
		//建立对应 的输入输出流对象
		FileOutputStream fileOutputStream = new FileOutputStream(file4);
		FileInputStream fileInputStream1 = new FileInputStream(file1);
		FileInputStream fileInputStream2 = new FileInputStream(file2);
		FileInputStream fileInputStream3 = new FileInputStream(file3);

		//创建序列流对象
		Vector<FileInputStream> vector = new Vector<FileInputStream>();
		vector.add(fileInputStream1);
		vector.add(fileInputStream2);
		vector.add(fileInputStream3);
		Enumeration<FileInputStream> e = vector.elements();

		SequenceInputStream sequenceInputStream = new SequenceInputStream(e);
		
		//读取文件数据
		byte[] buf = new byte[1024];
		int length = 0; 
		
		while((length = sequenceInputStream.read(buf))!=-1){
			fileOutputStream.write(buf,0,length);
		}
		
		//关闭资源
		sequenceInputStream.close();
		fileOutputStream.close();
		
	}
	
	
	
//	使用SequenceInputStream合并文件。
	public static void merge2() throws IOException{
		//找到目标文件
		File inFile1 = new File("F:\\a.txt");
		File inFile2 = new File("F:\\b.txt");
		File outFile = new File("F:\\c.txt");
		//建立数据的输入输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(outFile);
		
		FileInputStream fileInputStream1 = new FileInputStream(inFile1);
		FileInputStream fileInputStream2 = new FileInputStream(inFile2);
		//建立序列流对象
		SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
		byte[] buf = new byte[1024];
		int length = 0 ; 
		
		while((length = inputStream.read(buf))!=-1){
			fileOutputStream.write(buf,0,length);
		}
		//关闭资源
		inputStream.close();
		fileOutputStream.close();

	}
	
	
	//需求:把a.txt与b.txt 文件的内容合并。
	public static void merge1() throws IOException{
		//找到目标文件
		File inFile1 = new File("F:\\a.txt");
		File inFile2 = new File("F:\\b.txt");
		File outFile = new File("F:\\c.txt");
		//建立数据的输入输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(outFile);
		FileInputStream fileInputStream1 = new FileInputStream(inFile1);
		FileInputStream fileInputStream2 = new FileInputStream(inFile2);
		
		//把输入流存储到集合中,然后再从集合中读取
		ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
		list.add(fileInputStream1);
		list.add(fileInputStream2);
		
		//准备一个缓冲数组
		byte[] buf = new byte[1024];
		int length = 0 ; 
		
		for(int i = 0 ; i< list.size() ; i++){
			FileInputStream fileInputStream = list.get(i);
			while((length = fileInputStream.read(buf))!=-1){
				fileOutputStream.write(buf,0,length);
			}
			//关闭资源
			fileInputStream.close();
		}
		fileOutputStream.close();
		
	}

}

二、对象流

对象的输入输出流 : 对象的输入输出流主要的作用是用于写对象的信息与读取对象的信息。
对象信息一旦写到文件上那么对象的信息就可以做到持久化了。

对象的输出流: ObjectOutputStream
对象的输入流: ObjectInputStream

class Address implements Serializable{
	
	String country; 
	
	String city;
	
	public Address(String country,String city){
		this.country = country;
		this.city = city;
	}
	
}


class User implements Serializable{
	
	private static final long serialVersionUID = 1L;

	String userName ;
	
	String password;
	
	transient int age;  // transient 透明
	
	Address address ;
	

	public User(String userName , String passwrod) {
		this.userName = userName;
		this.password = passwrod;
	}
	
	
	public User(String userName , String passwrod,int age,Address address) {
		this.userName = userName;
		this.password = passwrod;
		this.age = age;
		this.address = address;
	}
	
	@Override
	public String toString() {
		return "用户名:"+this.userName+ " 密码:"+ this.password+" 年龄:"+this.age+" 地址:"+this.address.city;
	}
}

public class Demo3 {
	
	public static void main(String[] args) throws IOException, Exception {
		writeObj();
//		readObj();
	}
	
	//把文件中的对象信息读取出来-------->对象的反序列化
	public static void readObj() throws  IOException, ClassNotFoundException{
		//找到目标文件
		File file = new File("F:\\obj.txt");
		//建立数据的输入通道
		FileInputStream fileInputStream = new FileInputStream(file);
		//建立对象的输入流对象
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		//读取对象信息
		User user = (User) objectInputStream.readObject(); //创建对象肯定要依赖对象所属的class文件。
		System.out.println("对象的信息:"+ user);
	}

	//定义方法把对象的信息写到硬盘上------>对象的序列化。
	public static void writeObj() throws IOException{
		//把user对象的信息持久化存储。
		Address address = new Address("中国","深圳");
		User user = new User("admin","123",15,address);
		//找到目标文件
		File file = new File("F:\\obj.txt");
		//建立数据输出流对象
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//建立对象的输出流对象
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		//把对象写出
		objectOutputStream.writeObject(user);
		//关闭资源
		objectOutputStream.close();
		
		
	}
}

对象输入输出流要注意的细节:

  1. 如果对象需要被写出到文件上,那么对象所属的类必须要实现Serializable接口。 Serializable接口没有任何的方法,是一个标识接口而已。
  2. 对象的反序列化创建对象的时候并不会调用到构造方法。
  3. serialVersionUID 是用于记录class文件的版本信息的,serialVersionUID这个数字是通过一个类的类名、成员、包名、工程名算出的一个数字。
  4. 使用ObjectInputStream反序列化的时候,ObjectInputStream会先读取文件中的serialVersionUID,然后与本地的class文件的serialVersionUID进行对比,如果这两个id不一致,那么反序列化就失败了。
  5. 如果序列化与反序列化的时候可能会修改类的成员,那么最好一开始就给这个类指定一个serialVersionUID,如果类已经指定的serialVersionUID,那么在序列化与反序列化的时候,jvm都不会再自己算这个class的serialVersionUID了。
  6. 如果一个对象某个数据不想被序列化到硬盘上,可以使用关键字transient修饰。
  7. 如果一个类维护了另外一个类的引用,那么另外一个类也需要实现Serializable接口。

注意:所有的集合都实现了Serializable接口。

使用transient来修饰变量,则该变量的信息就不会写到硬盘上去。

三、打印流(printStream)

打印流可以打印任意类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印。

class Animal{
	
	String name;
	
	String color;
	
	public Animal(String name,String color){
		this.name = name;
		this.color = color;
	}
	
	@Override
	public String toString() {
		return "名字:"+this.name+ " 颜色:"+ this.color;
	}
	
}

public class Demo {
	
	public static void main(String[] args) throws IOException {
		/*FileOutputStream fileOutputStream = new FileOutputStream("F:\\a.txt");
		fileOutputStream.write(97);//写过去的是a
		fileOutputStream.write("97".getBytes());//写过去的是97
		fileOutputStream.close();*/
		
		
		//打印流可以打印任何类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印。
		File file = new  File("F:\\a.txt");
		//创建一个打印流
		PrintStream printStream = new PrintStream(file);
		/*
		printStream.println(97); //97
		printStream.println(3.14);//3.14
		printStream.println('a');//a
		printStream.println(true);//
		Animal a = new Animal("猪", "粉色"); 
		printStream.println(a);//名字:猪 颜色:粉色
		
		
		//默认标准的输出流就是向控制台输出的,
		System.setOut(printStream); //重新设置了标准的输出流对象 让内容输出到了PrintStream(file);文件中。
		System.out.println("Hello World!!");
		*/
		
		//收集异常的日志信息。
		File logFile = new File("F:\\log.log");
		//追加内容
		PrintStream logPrintStream = new PrintStream( new FileOutputStream(logFile,true) );
		try{
			int c = 4/0;
			System.out.println("c="+c);
			int[] arr = null;
			System.out.println(arr.length);
			
		}catch(Exception e){
			e.printStackTrace(logPrintStream);
			
		}
		
	}

}

四、转换流

输入字节流的转换流:
InputStreamReader 是字节流通向字符流的桥

注意:FileReader本身不能指定编码表,默认使用的是GBK码表;使用InputStreamReader(InputStream in,String charsetName)该构造方法可以指定具体的码表。

输出字节流的转换流:
OutputStreamWriter 可以把输出字节流转换成输出字符流

转换流的作用:

  1. 如果目前所获取到的是一个字节流需要转换字符流使用,此时就可以使用转换流。 (字节流----> 字符流)
  2. 使用转换流可以指定编码表进行读写文件。
public class Demo8 {
	
	public static void main(String[] args) throws IOException {
//		readTest();
//		writeTest();'
		
//		writeTest2();
		readTest2();
	}
	
	//使用输入字节流的转换流指定码表进行读取文件数据
	public static void readTest2() throws IOException{
		File file = new File("F:\\a.txt");
		FileInputStream fileInputStream = new FileInputStream(file);
		//创建字节流的转换流并且指定码表进行读取
		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
		char[] buf = new char[1024];
		int length = 0;
		while((length = inputStreamReader.read(buf))!=-1){
			System.out.println(new String(buf,0,length));
		}
		
	}

	
	//使用输出字节流的转换流指定码表写出数据
	public static void writeTest2() throws IOException{
		File file = new File("F:\\a.txt");
		//建立数据的输出通道
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//把输出字节流转换成字符流并且指定编码表。
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
		outputStreamWriter.write("你好世界");
		//关闭资源
		outputStreamWriter.close();
		
	}

	public static void writeTest() throws IOException{
		File file = new File("F:\\a.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//把输出字节流转换成输出字符流。
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
		
		outputStreamWriter.write("你好世界");  
		outputStreamWriter.close();
		
	}
	
	public static void readTest() throws IOException{
		InputStream in = System.in; //获取了标准的输入流。
//		System.out.println("读取的字符:"+ (char)in.read());  //读取控制台的内容,read()一次只能读取一个字节。

		//把字节流转换成字符流。
		InputStreamReader inputStreamReader = new InputStreamReader(in);
		//使用字符流的缓冲类
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		String line = null;
		while((line = bufferedReader.readLine())!=null){
			System.out.println("内容:"+ line);
		}
		
		
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值