Java基础之106 打印流 107 对象流与序列化 108字节数组流 109 数据流

106 打印流
打印流的主要功能是用于输出,在整个IO包中打印流分为两种类型:
字节打印流:PrintStream
字符打印流:PrintWriter
打印流可以很方便的进行输出

只是方便输出
增强功能
跟加了buff似的
写还是原来的写Writer out = new FileWriter(file);
套上一层 加上缓存功能 加了蓝爸爸 BufferedWriter bos = new BufferedWriter(out);
再套上一层 增强打印功能 加了红爸爸 PrintWriter pw = new PrintWriter(bos);

package com.vince;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * 打印流:很方便的进行输出
 * 
 * 字节打印流
 * 在字节输出时,可以增强输出功能
 * 字符打印流
 * 
 * @author vince
 * @description
 */
public class PrintStreamDemo {
	
	
	private static void charPrint(){
		File file = new File("c:\\test\\vince.txt");
		try {
			Writer out = new FileWriter(file);
			//加缓存
			BufferedWriter bos = new BufferedWriter(out);
			//增强打印功能
			PrintWriter pw = new PrintWriter(bos);
			pw.println("小河流水天天哗啦啦");
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void bytePrint(){
		File file = new File("c:\\test\\vince.txt");
		try {
			OutputStream out = new FileOutputStream(file);
			//加缓存
			BufferedOutputStream bos = new BufferedOutputStream(out);
			//增强打印功能
			PrintStream ps = new PrintStream(bos);
			ps.println("小河流水天天哗啦啦");
			ps.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		bytePrint();
		charPrint();
	}
}

107 对象流与序列化

对象流的两个类:
ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream
ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。

序列化一组对象:
在序列化操作中,同时序列化多个对象时,反序列化也必须按顺序操作,如果想要序列化一组对象该如何操作呢?
序列化一组对象可采用:对象数组的形式,因为对象数组可以向Object进行转型操作。

transient关键字:
如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。

package com.vince;

import java.io.Serializable;

//如果一个类创建的对象,需要被序列化,那么该类必须实现Serializable接口,
//Serializable是一个标记接口,没有任何定义,为了告诉JVM该类对象可以被序列化

//什么时候对象需要被序列化呢?
//1、把对象保存到文件中(存储到物理介质)
//2、对象需要在网络上传输
//如果对象没有实现Serializable接口,会报错误:java.io.NotSerializableException
public class Dog implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private String sex;
	private transient int 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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Dog(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public Dog() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Dog [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}
package com.vince;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class ObjectStreamDemo {
	
	
	/**
	 * 对象序列化
	 * 把对象写入文件:实际写入的是类名、属性名、属性类型、属性的值等
	 */
	private static void writeObjects(){
		Dog dog = new Dog("wangwang",2,"母");
		Dog dog2 = new Dog("2哈",3,"公");
		Dog[] dogs = {dog,dog2};
		File file = new File("c:\\test\\dog.obj");
		try {
			OutputStream out = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(out);
			oos.writeObject(dogs);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 反序列化的过程
	 * 从文件中把对象的内容读取出来,还原成对象
  *如果是一组对象 需要用对象数组存Dog[] dogs = (Dog)ois.readObject();
	 */
	private static void readObject(){
		File file = new File("c:\\test\\dog.obj");
		try {
			InputStream in = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(in);
			Dog dog = (Dog)ois.readObject();
			ois.close();
			System.out.println(dog);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 对象序列化
	 * 把对象写入文件:实际写入的是类名、属性名、属性类型、属性的值等
	 */
	private static void writeObject(){
		Dog dog = new Dog("wangwang",2,"母");
		File file = new File("c:\\test\\dog.obj");
		try {
			OutputStream out = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(out);
			oos.writeObject(dog);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		writeObject();
		readObject();
	}

}

108字节数组流

ByteArrayInputStream
包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read 方法要提供的下一个字节。 关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。

ByteArrayOutputStream
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。 关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。
跟文件没有关系 它是在内存上运行的 跟文件没有交互 仅仅是利用了流的读取数据的这种机制 它可以一个字节一个字节去处理字符串
题目 从下列字符串提取出英文字母 不分大小写
字节 ASCII码

在这里插入图片描述


package com.vince;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * 字节数组流:
 * 基于内存操作,内部维护着一个字节数组,我们可以利用流的读取机制来处理字符串
 * 无需关闭
 * @author vince
 * @description
 */
public class ByteArrayStreamDemo {

	
	private static void byteArray(){
		
		String s = "12345676dfghjhg(*$$%^&SDFGHJ";
		ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int curr = -1;//每次读取的字节
		while((curr=bais.read())!=-1){
			if((curr>=65 && curr<=90) || (curr>=97 && curr<=122)){
				baos.write(curr);//将符合条件的字节数写到输出流数组
			}
		}
		//此时无需关闭,原因,字节数组流是基于内存的操作流
		System.out.println(baos.toString());
		
	}
	public static void main(String[] args) {
		byteArray();
	}

}

109 数据流

DataInputStream:
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 DataInputStream 对于多线程访问不一定是安全的。 线程安全是可选的,它由此类方法的使用者负责。

DataOutputStream:
数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。

好处 跟机器无关 完全按照Java数据类型字节数来写的
缺点 要完全按照写入的顺序读出来 还浪费内存

package com.vince;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 数据流
 * 与机器无关的操作JAVA的基本数据类型
 * @author vince
 * @description
 */
public class DataStreamDemo {
	
	private static void read(){
		File file = new File("c:\\test\\vince.dat");
		try {
			InputStream in = new FileInputStream(file);
			BufferedInputStream bis = new BufferedInputStream(in);
			DataInputStream dis = new DataInputStream(bis);
			int num = dis.readInt();
			byte b = dis.readByte();
			String s = dis.readUTF();
			System.out.println(num+","+b+","+s);
			
			dis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	private static void write(){
		
		File file = new File("c:\\test\\vince.dat");
		try {
			OutputStream out = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(out);
			DataOutputStream dos = new DataOutputStream(bos);
			dos.writeInt(10); //写入4个字节 按照八种基本数据类型写入 
			dos.writeByte(1);//写入1个字节 所以int类型四个字节
			dos.writeUTF("中");
			
			dos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		write();
		read();
	}

}

猜你喜欢

转载自blog.csdn.net/C_time/article/details/89421784