学习笔记——day15(IO)

在这里插入图片描述

ObjectInputStream

读取对象

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

public class Test01 {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		//创建对象
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
		
		//读取数据
		int num = ois.readInt();
		
		
		//输出数据
		System.out.println(num);
		
		
		
		//读取数据
		double d = ois.readDouble();
		
		
		//输出数据
		System.out.println(d);
		
		//读取数据
		String s = ois.readUTF();
		
		
		//输出数据
		System.out.println(s);
		
		
		
		//关闭
		ois.close();
		
	}
}

ObjectOutputStream

写出对象

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


public class Test01 {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		//创建对象
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
		
		//写出数据
		//oos.write(123);
		//oos.writeByte(12);
		oos.writeInt(123);
		oos.writeDouble(12.12);
		oos.writeUTF("hello 少年");
		
		//关闭
		oos.close();
		
		
		
		
		
	}
}

import java.io.Serializable;

public class Lazy implements Serializable{
	
	private static Lazy lazy = null;
	
	private Lazy() {
		
	}
	
	public static Lazy getInstance() {
		if(lazy==null) {
			lazy = new Lazy();
		}
		return lazy;
	}
	
	// 当程序在序列化的时候 会默认调用类中的 readResolve方法
	// 不重写该方法 导致每次返回的是一个当前类的对象的副本,这个副本是一个新对象
	//  
	public Object readResolve() {
		return lazy;
	}
	
}

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

/*
 *    		将一个对象写出到文件中去		
 *    		在将这个对象读取进来
 *    
 *    
 * 	java.io.NotSerializableException
 * 	ObjectOutputStream 对象输出流写出对象是要保证当前对象可以序列化
 * 	transient 的作用就是序列化的时候,不会将当前属性对应的值序列化出去
 *  	序列化的时候记得增加序列化版本号
 * 
 * 
 * 
 */
public class Test {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		
		
		
		read();
		
		
	}
	
	public static void write() throws IOException {
		//创建对象
		User u = new User("zhangsan",1,18);
		
		
		//创建对象
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.a"));
		
		
		//写出
		oos.writeObject(u);
		
		//关闭
		oos.close();
	}
	
	public static void read() throws IOException, ClassNotFoundException {
		//读取数据
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.a"));
		
		Object obj = ois.readObject();
		System.out.println(obj);
		
		
		ois.close();
	}
	
}

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

public class Test01 {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Lazy lazy = Lazy.getInstance();
		
		//创建对象
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.a"));
		
		
		//写出
		oos.writeObject(lazy);
				
		
		//读取
		//读取数据
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.a"));
		
		Object obj = ois.readObject();
		
		
		System.out.println(obj);
		System.out.println(lazy);
	}
}

import java.io.Serializable;

public class User implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private int gender;
	private transient int age;
	
	
	public User() {
		// TODO Auto-generated constructor stub
	}

	
	
	
	public User(String name, int gender, int age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}




	@Override
	public String toString() {
		return "User [name=" + name + ", gender=" + gender + "]";
	}
	
}

System 对于InputStream的支持:

  • System.in: 标准输入流: 标准输入设备是键盘 指定了数据源
  • System.out: 标准输出力: 标准输出设备是控制台 指定输出的位置
  • System.err: 标准错误输出流
public class Test01 {
	public static void main(String[] args) throws FileNotFoundException {
		
		//修改标准输入流的数据源
		System.setIn(new FileInputStream("a.txt"));
		
		//Scanner对于IO的支持
		Scanner input = new Scanner(System.in);
		
		//通过Scanner获取输入数据
		System.out.println(input.next());
		
		//设置标准输出设置到文件中
		System.setOut(new PrintStream(new File("c.txt")));
		
		//通过打印 输出内容
		System.out.println("hiehie");
		
		try {
			input.close();
			input.next();
		}catch(IllegalStateException e) {
			System.out.println(e.getMessage());
		}
		
		
	}
}

public class Test02 {
	public static void main(String[] args) throws IOException, InterruptedException {
		
		Runtime run = Runtime.getRuntime();
		
		Process p = run.exec("java -version");
		
		 
		InputStream is = p.getErrorStream();
		
		/*
		 * BufferedReader br = new BufferedReader(new InputStreamReader(is));
		 * 
		 * String str = ""; StringBuilder sb = new StringBuilder();
		 * while((str=br.readLine())!=null) { sb.append(str); sb.append("\r\n"); }
		 */
		
		byte[] buf = new byte[1024*10];
		int len = is.read(buf);
		
		System.out.println(new String(buf,0,len));
		
		
	}
}

Reader: 所有字符输入流的父类

InputStreamReader: 字节字符输入流

读一个
public class Test01 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		Reader reader = new InputStreamReader(new FileInputStream("a.txt"));
		
		//读取
		int num = reader.read();
		
		//分析结果
		System.out.println(num);
		
		//关闭
		reader.close();
		
		
		//System.out.println((char)(new FileInputStream("a.txt").read()));
		
	}
}

循环读
public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		Reader reader = new InputStreamReader(new FileInputStream("C:\\Users\\wawjy\\Desktop\\c.txt"));
		
		//读取
		int num = 0;
		
		while((num=reader.read())!=-1) {
			System.out.print((char)num);
		}
		
		
		
		//关闭
		reader.close();
		
		
		//System.out.println((char)(new FileInputStream("a.txt").read()));
	}

}

读多个

public class Test04 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		Reader reader = new InputStreamReader(new FileInputStream("c.txt"),"utf-8");
		
		//声明字符数组
		char[] chs = new char[1024];
		
		//将读取到的信息存放在chs字符数组中 返回读取大文件的字符个数
		int count = reader.read(chs);
		
		
		System.out.println(new String(chs, 0, count));
		
		
		//关闭
		reader.close();
		
		
		//System.out.println((char)(new FileInputStream("a.txt").read()));
		
		
		
	}

}

异常
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class Test05 {
	static String filePath = "C:\\Users\\wawjy\\Desktop\\abc.a";
	
	public static void main(String[] args) {
		
		write("鹅鹅鹅\n曲项向天歌\n白毛浮绿水\n红掌拨清波");
		read();
		
	}
	
	
	public static void read() {
		
		//1:创建对象
		Reader out = null;
				
		try {
			out = new InputStreamReader(new FileInputStream(filePath),"GBK");
			
			char[] chs = new char[1024];
			int count = 0;
			StringBuilder sb = new StringBuilder();
			
			while((count=out.read(chs))!=-1) {
				sb.append(chs,0, count);
			}
			
			System.out.println(sb.toString());
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(out!=null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	
	
	
	
	
	public static void write(String msg) {
		//1:创建对象
		Writer out = null;
				
		try {
			out = new OutputStreamWriter(new FileOutputStream(filePath),"GBK");
			
			//输出内容
			out.write(msg);
			
			//刷新
			out.flush();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(out!=null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}
}
BufferedRead
public class Test06 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		BufferedReader reader = new BufferedReader(
					new InputStreamReader(
					new FileInputStream("a.txt")));
		
		//读取
		String str = null;
		StringBuilder sb = new StringBuilder();
		
		while((str=reader.readLine())!=null) {
			sb.append(str);
			sb.append("\r\n");//保留格式
		}
		
		//读数据
		System.out.println(sb.toString());
		
		//关闭
		reader.close();
		
		
		
	}
}

字符流Writer:

  • Writer: 是所有字符输出流的父类
  • OutputStreamWriter:字符输出流
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Test01 {
	public static void main(String[] args) throws IOException {
		
		//创建流对象
		OutputStream os = new FileOutputStream("a.txt");
		Writer out = new OutputStreamWriter(os);
		
		//声明数据
		char ch = '帅';
		
		//输出数据
		out.write(ch);
		
		//刷新
		out.flush();
		
		//关闭
		out.close();
		
	}
}

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		Writer out = new OutputStreamWriter(new FileOutputStream("C:\\Users\\wawjy\\Desktop\\c.txt"));
		
		//声明输出数据
		String str = "你a好a";
		
		//输出
		//out.write(str.toCharArray());
		out.write(str);
		
		//刷新
		out.flush();
		
		//关闭
		out.close();
		
	}
}

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Test03 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		Writer out = new OutputStreamWriter(new FileOutputStream("C:\\Users\\wawjy\\Desktop\\c.txt"));
		
		//声明输出数据
		String str = "为什么不行呀?????";
		
		//输出
		out.write(str.toCharArray());
		//out.write(str);
		
		//刷新
		out.flush();
		
		//关闭
		out.close();
		
	}
}

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Test04 {
	public static void main(String[] args) throws IOException {
		
		Writer out = new OutputStreamWriter(new FileOutputStream("C:\\Users\\wawjy\\Desktop\\c.a"),"GBK");
		
		String str = "再不行我就去。�?��??";
		
		out.write(str);
		
		out.flush();
		
		out.close();
		
	}
}

BufferedWrite

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/*
 * 
 *  bufferedwriter 效率更高  
 * 
 */
public class Test05 {
	public static void main(String[] args) throws IOException {
		
		//创建对象
		OutputStream os = new FileOutputStream("a.txt");
		
		Writer out = new OutputStreamWriter(os);
		
		BufferedWriter bw = new BufferedWriter(out);
		
		
		//写出数据
		bw.write("我自横刀向天笑");
		bw.newLine();
		bw.write("去留肝胆两昆仑");
		
		
		bw.flush();
		
		bw.close();
		out.close();
		os.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值