I/O框架、对象序列化


在这里插入图片描述

  I/O 流结构。

在这里插入图片描述

在这里插入图片描述

  InputStream、OutputStream,字节流。
  FileInputStream 字节节点输入流。

// 字节读取
public class Test1 {

	public static void main(String[] args) throws IOException {
		//声明流对象
		InputStream is=null;
		try {
			//1.创建流对象,文件不存在会运行失败
			is=new FileInputStream("a1.txt");
			//2.用流对象读取文件中内容,一个字节一个字节读取,返回的是读取内容ascii码
			//先读取一次
			int content=is.read();
			while (content!=-1) {
				//将读取的字节转换为char类型内容再输出
				System.out.print((char)content);
				//接着读
				content=is.read();
			}
			System.out.println("\n读取完毕");
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (is!=null) {
				is.close();
			}
		}
	}
}

// 采用字节数组,一次读取多个字节
public class Test2 {

	public static void main(String[] args) throws IOException {
		//声明流对象
		InputStream is=null;
		try {
			//1.创建流对象
			is=new FileInputStream("a1.txt");
			//2.用流对象读取文件中内容,一个数组一个数组的读取,读取的内容存在数组中,返回的是读取内容长度
			//准备一个数组
			byte[] b=new byte[10];
			//先读取一次
			int len=is.read(b);
			while (len!=-1) {
				//将读取的字节数组内容转换为String,再输出
				String s1=new String(b, 0, len);
				System.out.println(s1);
				//接着读
				len=is.read(b);
			}
			System.out.println("\n读取完毕");
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (is!=null) {
				is.close();
			}
		}
	}
}

  用字节节点流实现拷贝文件功能。


public class Test {

	public static void main(String[] args) throws IOException {
		//声明流对象
		InputStream is=null;
		OutputStream os=null;
		try {
			//1.创建流对象,拷贝后的文件与原文件类型相同(后缀名一样)
			is=new FileInputStream("a1.jpg");
			os=new FileOutputStream("a2.jpg");

			//准备一个数组
			byte[] b=new byte[10];
			//先读取一次
			int len=is.read(b);
			while (len!=-1) {
				//每读取一次就写入一次
				os.write(b, 0, len);
				//接着读取
				len=is.read(b);
			}
			System.out.println("拷贝成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (is!=null) {
				is.close();
			}
			if (os!=null) {
				os.close();
			}
		}
	}
}

  BufferedOutputStream、BufferedInputStream,字节过滤流。提高I/O效率,减少访问磁盘的次数;数据存储在缓冲区中,flush() 是将缓存区的内容写入文件中,也可以直接close()。


// 字节过滤输出流的使用
public class BufferedOutputStreamTest {

	public static void main(String[] args) throws IOException {
		//声明流对象
		BufferedOutputStream bos=null;
		try {
			//1.创建流对象
			bos=new BufferedOutputStream(new FileOutputStream("b1.txt",true));
			//2.用流对象向文件中写入内容,按一个数组来写入
			bos.write("dfefefefefef".getBytes());
			System.out.println("写入成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (bos!=null) {
				bos.close();
			}
		}
	}
}

// 字节过滤输入流的使用
public class Test {

	public static void main(String[] args) throws IOException {
		//声明流对象
		BufferedInputStream bis=null;
		try {
			//1.创建流对象
			bis=new BufferedInputStream(new FileInputStream("b1.txt"));
			//2.用流对象读取文件中内容,按一个数组来读取
			//准备一个数组
			byte[] b=new byte[10];
			//声明一个变量存每次读取的长度
			int len;
			while ((len=bis.read(b))!=-1) {
				//将读取的内容转换为String并输出
				System.out.println(new String(b, 0, len));
			}
			
			System.out.println("读取成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (bis!=null) {
				bis.close();
			}
		}
	}
}

  Reader、Writer,字符流。
  FileReader、FileWriter,字符节点流。


public class Test {

	public static void main(String[] args) throws IOException {
		//声明流对象
		FileReader r=null;
		FileWriter w=null;
		try {
			//1.创建流对象
			r=new FileReader("a1.txt");
			w=new FileWriter("b1.txt");
			//2.边读取边拷贝,按字符数组拷贝
			char[] c=new char[100];
			//声明一个变量存读取内容长度
			int len;
			while ((len=r.read(c))!=-1) {
				//每读取一次就写入一次
				w.write(c, 0, len);
			}
			System.out.println("拷贝成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (r!=null) {
				r.close();
			}
			if (w!=null) {
				w.close();
			}
		}
	}
}

  InputStreamReader、OutputStreamWriter,桥转换流。可将字节流转换为字符流。可设置字符的编码方式。

public class Test {

	public static void main(String[] args) throws IOException {
		//声明流对象
		InputStreamReader r=null;
		OutputStreamWriter w=null;
		try {
			//1.创建流对象
			r=new InputStreamReader(new FileInputStream("a1.txt"), "utf-8");
			w=new OutputStreamWriter(new FileOutputStream("b2.txt"), "utf-8");
			//2.边读取边拷贝,按字符数组拷贝
			char[] c=new char[100];
			//声明一个变量存读取内容长度
			int len;
			while ((len=r.read(c))!=-1) {
				//每读取一次就写入一次
				w.write(c, 0, len);
			}
			System.out.println("拷贝成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (r!=null) {
				r.close();
			}
			if (w!=null) {
				w.close();
			}
		}
	}
}

  BufferedWriter、BufferedReader,字符缓冲流。支持输入换行符。可一次写一行、读一行。

public class Test {

	public static void main(String[] args) throws IOException {
		//声明流对象
		BufferedReader r=null;
		BufferedWriter w=null;
		try {
			//1.创建流对象
			r=new BufferedReader(new InputStreamReader(new FileInputStream("a1.txt"), "utf-8"));
			w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a2.txt"), "utf-8"));
			
			//2.边读取边写入,一行一行的读取
			//声明一个变量存每次读取内容
			String s1;
			while ((s1=r.readLine())!=null) {
				//每读取一次就写入一次
				w.write(s1);
				//每写入一次就换
				w.newLine();
				//刷新
				w.flush();
			}
			
			System.out.println("拷贝成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (r!=null) {
				r.close();
			}
			if (w!=null) {
				w.close();
			}
		}
	}

}

  PrintStream/PrintWriter,字节打印流/字符打印流。封装了 print() 、 println() 方法,支持写入后换行。支持数据原样打印

public class PrintStreamTest1 {

	public static void main(String[] args) {
		//声明流对象
		// PrintWriter ps=null;
		PrintStream ps=null;
		try {
			//1.创建流对象
			// ps=new PrintWriter("aa.txt");
			ps=new PrintStream("aa.txt");
			//2.用流对象调用方法向文件中写入内容 
			ps.write("Hellow".getBytes());
			ps.append("World");
			ps.println("java");
			ps.printf("%.2f", 3.343434545454);
			ps.print("html");
			System.out.println("写入成功!");
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (ps!=null) {
				ps.close();
			}
		}
	}
}

  setIn、setOut,重定向。


public class Test {

	public static void main(String[] args) throws FileNotFoundException {
		//输入重定向,输入的字符被文件接收
		System.setIn(new FileInputStream("bb.txt"));
		
		Scanner input=new Scanner(System.in);
		System.out.println("请输入一个字符串:");
		String s1=input.next();
		String s2=input.next();
		System.out.println("接收的字符串为:"+s1);
		System.out.println("接收的字符串为:"+s2);
	}
}

public class Test {

	public static void main(String[] args) throws FileNotFoundException {
		//输出重定向,输出的字符被文件接收
		System.setOut(new PrintStream("bb.txt"));
		
		System.out.println("想要月薪过万");
		System.out.println("键盘敲烂");
	}

}

  ObjectOutputStream、ObjectInputStream,对象流,属于字节过滤流。增强了缓冲区功能,增强了读写8种基本数据类型和字符串功能,增强了读写对象的功能。
  使用流传输对象的过程称为序列化、反序列化。
  对象序列化,必须实现 Serializable 接口;必须保证其所有属性均可序列化;transient 修饰为临时属性,不参与序列化;读取到文件尾部的标志,java.io.EOFException。

  声明序列化类,必须实现 Serializable 接口。如果序列化后还会更改类的属性,要固定序列化版本号。如果序列化类中某个属性不想被序列化可以用 static 或 transient 修饰。

public class Student implements Serializable {

	// 固定序列化版本号
	private static final long serialVersionUID = 9182661412185050266L;
	
	private String sname;
	//如果序列化类中某个属性不想被序列化可以用static或transient修饰
	private transient int sage;
	
	@Override
	public String toString() {
		return "Student [sname=" + sname + ", sage=" + sage + "]";
	}
	
	public Student() {
		
	}
	
	public Student(String sname, int sage) {
		super();
		this.sname = sname;
		this.sage = sage;
	}

	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getSage() {
		return sage;
	}
	public void setSage(int sage) {
		this.sage = sage;
	}
	
}

  向流中写入序列化对象。


public class Test1 {

	public static void main(String[] args) throws IOException {
		//声明流对象
		ObjectOutputStream oos=null;
		
		try {
			//1.创建流对象
			oos=new ObjectOutputStream(new FileOutputStream("cc.txt"));
			//2.用流向文件中写入对象
			//第一种:写入一个普通对象
//			String s1="饿了吗?";
//			oos.writeObject(s1);
			
			//第二种:写入一个自定义对象
			oos.writeObject(new Student("kk", 6));
			
			//第三种:将多个对象存入到集合中,再将集合写入到时文件中
			List<Student> stuList=new ArrayList();
			stuList.add(new Student("kk1", 18));
			stuList.add(new Student("kk2", 28));
			stuList.add(new Student("kk3", 38));
			stuList.add(new Student("kk4", 48));
			
			oos.writeObject(stuList);

			System.out.println("写入成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (oos!=null) {
				oos.close();
			}
		}
	}
}

  读取序列化对象。读写使用的方法类型应一致。


public class Test2 {

	public static void main(String[] args) throws IOException {
		//声明流对象
		ObjectInputStream ois=null;
		
		try {
			//1.创建流对象
			ois=new ObjectInputStream(new FileInputStream("cc.txt"));
			//2.用流读取文件中对象
			//第一种:读取一个普通对象
//			String s1=(String) ois.readObject();
//			System.out.println("读取的内容为:"+s1);
			
			//第二种:读取一个自定义对象
			Student stu=(Student) ois.readObject();
			System.out.println(stu);
			
			//第三种:读取集合
			List<Student> stuList=(List<Student>) ois.readObject();
			for (Student s : stuList) {
				System.out.println(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//3.关流
			if (ois!=null) {
				ois.close();
			}
		}

	}
}

  Properties 读取配置文件。


// 文件名后缀必须为 properties,健=值
// .class.getClassLoader() 方法直接从当前 bin 目录中取文件,方便其它位置调用

public class PropertiesTest {

	public static void main(String[] args) throws IOException {
		//创建Properties对象
		Properties p=new Properties();
		//读取配置文件
		p.load(PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties"));
		
		//获取配置文件中信息
		System.out.println("url:" + p.getProperty("url"));
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值