Java基础知识补充(io小案例笔记)

1、字符流

分析以下程序:

public static void out() throws IOException{
		//1、创建流
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d.txt")));
		
		//3、写出
		bw.write("hello world");
		bw.newLine();
		bw.write("hello java");
		//4、刷新
		bw.flush();
		//5、关闭
		bw.close();
	}

小知识点:对于通过字符流写出时,在管道的中间会有一个字符的转换的缓冲,所以需要手动的将缓冲中的内容刷新到磁盘。如果不进行刷新,可能数据没有写入到磁盘上。当然close()方法也会有刷新的功能,但是程序一般不使用close()去刷新缓存。只使用它进行关闭流操作。

字符流分析图

2、对象流:

对象流的作用:将对象写出到磁盘,将对象从磁盘读取。

小案例:

public static void in()throws IOException{
		//1、创建流
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.ora"));
		//2、读取数据
		int num = ois.readInt();
		boolean flag = ois.readBoolean();
		//3、分析数据
		System.out.println(flag);
		System.out.println(num);
		//4、关闭
		ois.close();
	}
	
	public static void out() throws IOException{
		//1、创建流
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.ora"));
		//2、写出
		oos.writeInt(123);
		oos.writeBoolean(true);
		oos.writeUTF("你好吗?");
		
		//3、关闭
		oos.close();
	}

注意事项:读取的顺序需要与写出的顺序保持一致。

小案例:

public class User implements Serializable {

	private String name;
	private transient String pwd;  //像pwd这样的数据不想要写入磁盘,可以加上transient。
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPwd() {
		return pwd;
	}
	
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public User(String name, String pwd) {
		super();
		this.name = name;
		this.pwd = pwd;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", pwd=" + pwd + "]";
	}
	
}
//测试对象流
public class Test {

	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//		out();
		in();
	}
	
	public static void in() throws FileNotFoundException, IOException, ClassNotFoundException{
		//创建流
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("User.txt")));
		//读取数据
		User u=(User)ois.readObject();
		//分析数据
		System.out.println(u);
		//关闭
		ois.close();
		
	}
	
	public static void out() throws FileNotFoundException, IOException{
		//创建流
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("User.txt")));
		
		//写出
		User user=new User("张三","123");
		
		oos.writeObject(user);
		//关闭
		oos.close();
	}
}

注意事项:对象流写入到磁盘:要保证要写入的对象实现了Serializable接口。

对于第二个案例分析:

如果我们在user类实例写入到磁盘之后,对User类进行了修改,那么我们在次从磁盘上读进程序是就会产生: io.User; local class incompatible: stream classdesc serialVersionUID = 2426914130122007738,local class serialVersionUID = -6441767697117996275异常。可知读入的对象的版本号与程序中User对应的版本号不一致,导致读入的对象不能强制转换为程序中的User。那么怎么解决这个问题呢?
解决:我们只需要在User类当中设置好版本号即可:private static final long serialVersionUID = 1L;

标准输入输出流

小案例:

public class Test {
	public static void main(String[] args) throws FileNotFoundException {
		
		System.setIn(new FileInputStream("d.txt"));//设置输出
		
		Scanner input =new Scanner(System.in);
		
		String str=input.next();
		
		//设置从程序写数据到指定目的地
		//System.setOut(new PrintStream(new FileOutputStream("d.txt")));
		
		System.out.println(str);
		
		
	}

	}

猜你喜欢

转载自blog.csdn.net/ToBe_Coder/article/details/81586885
今日推荐