学会Java输入输出流,看这一篇就够了,建议收藏!

1、流的概念

Java 语言采用流的机制来实现输入/输出。
所谓流,就是数据的有序排列。
而流可以是从某个源(称为流源,Source of Stream)出来,
到某个目的地(称为流汇 ,Sink of Stream)去的。

1.1、流的分类

  1. 流向分:输入流、输出流
  2. 流源分:字符流、字节流
  3. 功能分:节点流、处理流

java 中统一将流的类放在 java.io 包下。

1.2、流的处理过程

用来处理流的类又叫做流处理器。

例如:Java 中可以使用 FileInputStream 从一个文件中读取数据,FileInputStream 就是一个流处理器

在这里插入图片描述

类似的也可以通过FileOutputStream向一个文件中写入数据。

在这里插入图片描述
Java 中提供了一种称作为链接的机制,可以将一个流处理器与另一个流处理器首尾相连,形成一个流管道的链接。
在这里插入图片描述
类似的也可以向一个文件中写入数据
在这里插入图片描述

2、常用流的使用

FileInputStream和FileOutputStream:用于读取和写入诸如图像数据之类的原始字节流
public static void main(String[] args){
    
    
	try{
    
    
		File oldFile=new File("D:\\SoftMgr\\img.jpg");//从本地获取文件对象
		File newFile=new File("D:\\SoftMgr\\new.jpg");//写入本地路径新文件
		FileInputStream in=new FileInputStream(oldFile);//用输入流读取文件的内容
		FileOutputStream out=new FileOutputStream(newFile);//用输出流把内容写入新文件
		int len=0;
		//如果没有读到文件某尾(-1代表读到文件某尾),输出流持续写
		while((len=in.read())!=-1){
    
    
			out.write(len);
		}
		//流用完后必须要关闭,先打开的后关闭,后打开的先关闭
		out.close();
		in.close();
	}catch(FileNotFoundException e){
    
    
		e.printStackTrace();
	}catch(IOException e){
    
    
		e.printStackTrace();
	}
}

ObjectInputStream和ObjectOutputStream:

对象流,ObjectOutputStream 和ObjectInputStream 分别与FileOutputStream 和FileInputStream 一起使用时,可以为应用程序提供对对象图形的持久存储。

ObjectInputStream 用于恢复那些以前序列化的对象。ObjectInputStream 对以前使用ObjectOutputStream 写入的基本数据和对象进行反序列化。

//将对象保存到文件必须实现Serializable序列化接口
@SuppressWarnings("serial")
public class User implements Serializable {
    
    
	private String name;
	private int age;
	public User(String name, int age) {
    
    
		super();
		this.name = name;
		this.age = age; 
	}
	public String getName() {
    
    
		return name; 
	}
	public int getAge() {
    
    
		return age; 
	} 
}
public class TestObjectStream {
    
    
	public static void writeObject(Object o) {
    
    
		FileOutputStream file;
		try {
    
    
			file = new FileOutputStream("E:\\img\\obj.dat");// 对象保存到本地.dat文件
			ObjectOutputStream out = new ObjectOutputStream(file);
			out.writeObject(o);
			out.flush();
			out.close();
		} catch (Exception e) {
    
    
			e.printStackTrace();
		} 
	}
	public static Object readObject(String path) {
    
    
		Object obj = null;
		try {
    
    
			FileInputStream file = new FileInputStream(path);
			ObjectInputStream in = new ObjectInputStream(file);
			obj = in.readObject();
			in.close();
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}
		return obj; 
	}
	public static void main(String[] args) {
    
    
		// writeObject(new User("jack", 35));
		User user = (User) readObject("E:\\img\\obj.dat");
		System.out.println("name:" + user.getName());
		System.out.println("age:" + user.getAge());
	} 
}

3、InputStream 和 OutputStream

在这里插入图片描述

4、Reader 和 Writer

在这里插入图片描述

5、节点流和处理流

节点流(原始流):可以从或向一个特定的地方(节点)读写数据。如FileReader.
处理流(链接流):是对一个已存在的流的连接和封装,
通过所封装的流的功能调用实现数据读写。
如BufferedReader处理流的构造方法总是要带一个其他的流对象做参数。
一个流对象经过其他流的多次包装,称为流的链接。

6、通过URL读取流

URL类:代表一个统一资源定位符,它是指向互联网“资源”的指针。
public static void main(String[] args){
    
    
	try{
    
    
		URL url=new URL("https://127.0.0.1:8090/SoftMgr/dzh.jpg");
		InputStream in=url.openStream();
		FileOutputStream out=new FileOutputStream("D:\\SoftMgr\\dyw.jpg");
		int len=0;
		byte[] bs=new byte[1024];
		while((len=in.read(bs))!=-1){
    
    
				out.write(bs,0,len);
		}
		out.flush();
		out.close();
		in.close();
	}catch(Exception e){
    
    
		e.printStackTrace();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42257666/article/details/120752022