JAVA18——IO(其它流、关闭流方法)

其它流

节点流

1.字节数组  字节  节点流

   输入流:ByteArrayInputStream  read(byte[] b, int off, int len) + close()
   输出流:ByteArrayOutputStream  write(byte[] b, int off, inet len) + toByteArray()  不要使用多态

/**
 * 字节数组  节点流
 * 数组的长度有限,数据量不会很大
 * 
 * 文件内容不用太大
 * 1.文件—程序—>字节数组
 * 2.字节数组—程序—>文件
 * @author qiao39gs
 *
 */
public class ByteArrayDemo01 {

	public static void main(String[] args) throws IOException {
		read(write());
	}
	
	/**
	 * 输入流  操作与文件输入流操作一致
	 * @throws IOException 
	 */
	public static void read(byte[] src) throws IOException{
		//数据源传入
		
		//选择流
		InputStream is = new BufferedInputStream(
					new ByteArrayInputStream(
								src
							)
				);
		//操作
		byte[] flush = new byte[1024];
		int len = 0;
		while(-1!=(len=is.read(flush))){
			System.out.println(new String(flush,0,len));
		}
		//释放资源
		is.close();
	}
	
	/**
	 * 输出流  操作与文件输出流有些不同,有新增方法,不能使用多态
	 * @throws IOException 
	 */
	public static byte[] write() throws IOException{
		//目的地
		byte[] dest;
		//选择流  不同点
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		//操作 写出
		String msg = "操作与文件输入流操作一致";
		byte[] info = msg.getBytes();
		bos.write(info, 0, info.length);
		//获取数据
		dest = bos.toByteArray();
		//释放资源
		bos.close();
		return dest;
	}
	
}

将文件读取到字节数组里面,同时将字节数组写出到文件中:

/**
 * 1.文件—程序—>字节数组
 * 		文件输入流
 * 		字节数组输出流
 * 2.字节数组—程序—>文件
 * 		字节数组输入流
 * 		文件输出流
 * @author qiao39gs
 *
 */
public class ByteArrayDemo02 {

	public static void main(String[] args) throws IOException {
		byte[] data =  getBytesFromFile("D:/WorkSpace/demo15/source/1.jpg");
		toFileFromByteArray(data,"D:/WorkSpace/demo15/source/arr.jpg");
		System.out.println(new String(data));
	}
	
	/**
	 * 1.文件—程序—>字节数组
	 * @param srcPath
	 * @return
	 * @throws IOException
	 */
	public static byte[] getBytesFromFile(String srcPath) throws IOException{
		//创建文件流
		File src = new File(srcPath);
		//创建字节数组目的地
		byte[] dest = null;
		
		//选择流
		//文件输入流
		InputStream is =  new BufferedInputStream(new FileInputStream(src));
		//字节数组输出流不能使用多态
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		//操作 不断读取文件 写出到字节数组流中
		byte[] flush = new byte[1024];
		int len = 0;
		while(-1!=(len=is.read(flush))){
			//写出到字节数组流中
			bos.write(flush, 0, len);
		}
		bos.flush();
		
		//获取数据
		dest = bos.toByteArray();
		
		bos.close();
		is.close();
		
		return dest;
	}
	
	/**
	 * 2.字节数组—程序—>文件
	 * @param src
	 * @param destPath
	 * @throws IOException
	 */
	public static void toFileFromByteArray(byte[] src, String destPath) throws IOException{
		//创建源
		//目的地
		File dest = new File(destPath);
		
		//选择流
		//字节数组输入流
		InputStream is = new BufferedInputStream(new ByteArrayInputStream(src));
		
		//文件输出流
		OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
		
		//操作不断读取字节数组
		byte[] flush = new byte[1024];
		int len = 0;
		while(-1!=(len=is.read(flush))){
			//写出到文件中
			os.write(flush, 0, len);
		}
		os.flush();
		
		//释放资源
		os.close();
		is.close();
	}
}

处理流

1.基本类型+String 保留数据+类型

   输入流:ByteArrayInputStream  read(byte[] b, int off, int len) + close()
   输出流:ByteArrayOutputStream  write(byte[] b, int off, inet len) + toByteArray()  不要使用多态

读写数据+类型(文件):

/**
 * 数据类型(基本+String)数据量
 * 1.输入流 DataInputStream	readXxx()
 * 2.输出流 DataOutputStream	writeXxx()
 * 
 * java.io.EOFException:没有读取到相关的内容
 * 新增方法不能使用多态
 * @author qiao39gs
 *
 */
public class DataDemo01 {

	public static void main(String[] args) throws IOException {
		read("D:/WorkSpace/demo15/source/arr.txt");
		write("D:/WorkSpace/demo15/source/arr.txt");
	}
	
	/**
	 * 数据+类型输出到文件
	 * @param destPath
	 * @throws IOException
	 */
	public static void write(String destPath) throws IOException{
		double point = 2.5;
		long num = 100L;
		String str = "数据类型";
		
		//创建源
		File dest = new File(destPath);
		//选择流	DataOutputStream
		DataOutputStream dos = new DataOutputStream(
					new BufferedOutputStream(
								new FileOutputStream(dest)
							)
				);
		//操作	写出的顺序为读取准备
		dos.writeDouble(point);
		dos.writeLong(num);
		dos.writeUTF(str);
		dos.flush();
		//释放资源
		dos.close();
	}
	
	/**
	 * 从文件读取数据+类型
	 * @param destPath
	 * @throws IOException
	 */
	public static void read(String destPath) throws IOException{
		//创建源
		File src = new File(destPath);
		//选择流
		DataInputStream dis = new DataInputStream(
					new BufferedInputStream(
								new FileInputStream(src)
							)
				);
		//操作读取的顺序与写出一致必须存在才能读取
		//不一致,数据存在问题
		double num1 = dis.readDouble();
		long num2 = dis.readLong();
		String str = dis.readUTF();
		
		System.out.println(num1);
		System.out.println(num2);
		System.out.println(str);
	}
	
}

读写数据+类型(字节数组):

public class DataDemo02 {

	public static void main(String[] args) throws IOException {
		byte[] data = write();
		System.out.println(data.length);
		read(data);
	}
	
	/**
	 * 数据+类型输出到字节数组中
	 * @param destPath
	 * @throws IOException
	 */
	public static byte[] write() throws IOException{
		//目标数组
		byte[] dest = null;
		double point = 2.5;
		long num = 100L;
		String str = "数据类型";
		
		//选择流	DataOutputStream
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(
					new BufferedOutputStream(
								bos
							)
				);
		//操作	写出的顺序为读取准备
		dos.writeDouble(point);
		dos.writeLong(num);
		dos.writeUTF(str);
		dos.flush();
		dest = bos.toByteArray();
		//释放资源
		dos.close();
		
		return dest;
	}
	
	/**
	 * 从字节数组读取数据+类型
	 * @param destPath
	 * @throws IOException
	 */
	public static void read(byte[] src) throws IOException{
		//选择流
		DataInputStream dis = new DataInputStream(
					new BufferedInputStream(
								new ByteArrayInputStream(src)
							)
				);
		//操作读取的顺序与写出一致必须存在才能读取
		//不一致,数据存在问题
		double num1 = dis.readDouble();
		long num2 = dis.readLong();
		String str = dis.readUTF();
		
		dis.close();
			
		System.out.println(num1);
		System.out.println(num2);
		System.out.println(str);
	}
	
}

2.引用类型(对象)保留数据+类型

   反序列化  输入流:ObjectInputStream  readObject()
   序列化  输出流:ObjectOutputStream  writeObject()
   注意:先序列化后反序列化,反序列化顺序必须与序列化一致
              不是所有的对象都可以序列化,java.io.Serializable
              不是所有的属性都需要序列化,transient

对象、数组的序列化和反序列化:

public class Employee implements java.io.Serializable {
	private transient String name;	//不需要序列化
	private double salary;
	
	public Employee() {		//空接口是该对象可以被序列化的标识
	}
	public Employee(String name, double salary) {
		super();
		this.name = name;
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}
/**
 * 不是所有的对象都可以序列化	java.io.NotSerializableException
 * 不是所有的属性都需要序列化
 * @author qiao39gs
 *
 */
public class ObjectDemo01 {
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		seri("D:/WorkSpace/demo15/source/ser.txt");
		read("D:/WorkSpace/demo15/source/ser.txt");
	}
	
	//序列化
	public static void seri(String destPath) throws IOException{
		Employee emp = new Employee("AA",10000);
		int[] arr = {1,2,3,4,5,6};
		//创建源
		File dest = new File(destPath);
		//选择流	ObjectOutputStream
		ObjectOutputStream dos = new ObjectOutputStream(
					new BufferedOutputStream(
								new FileOutputStream(dest)
							)
				);
		//操作	写出的顺序为读取准备
		dos.writeObject(emp);
		dos.writeObject(arr);
		//释放资源
		dos.close();
	}
	
	//反序列化
	public static void read(String destPath) throws IOException, ClassNotFoundException{
		//创建源
		File src = new File(destPath);
		//选择流
		ObjectInputStream dis = new ObjectInputStream(
					new BufferedInputStream(
								new FileInputStream(src)
							)
				);
		//操作读取的顺序与写出一致必须存在才能读取
		//不一致,数据存在问题
		Object obj = dis.readObject();
		if(obj instanceof Employee){
			Employee emp = (Employee)obj;
			System.out.println(emp.getName());
			System.out.println(emp.getSalary());
		}
		obj = dis.readObject();
		int[] arr = (int[])obj;
		System.out.println(Arrays.toString(arr));
		dis.close();
			
	}
}

关闭流方法

public class FileUtil {
	
	/**
	 * 工具类关闭流
	 * @param info
	 * @param io
	 */
	//...表示可变参数,只能在形参的最后一个位置,处理方式与数组一致
	public static void close(String info,Closeable ... io){	
		for(Closeable temp:io){
			try {
				if(null!=temp){
					temp.close();
				}
			} catch (Exception e) {
			}
		}
	}
	
	/**
	 * 使用泛型方法
	 */
	public static <T extends Closeable> void closeAll(T ... io){	
		for(Closeable temp:io){
			try {
				if(null!=temp){
					temp.close();
				}
			} catch (Exception e) {
			}
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值