第五课 其他流

1 节点流:字节数组输入输出流

  • 字节数组节点流(字节)
    • 输入流:ByteArrayInputStream
    • 输出流:ByteArrayOutputStream
public static void test01() {
	// 数据源
	String str = "abcdefghijklmnopqrstuvwxyz";
	String tmp_str = null;
	byte[] src = new byte[100];
	byte[] tmp = new byte[100];
	int len = 0;
	src = str.getBytes();

	InputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(src));
	try {
		while ((len = inputStream.read(tmp)) != -1) {
			tmp_str = new String(tmp, 0, len);
			System.out.println(tmp_str);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			// close()方法在ByteArrayInputStream中没有用,本身读取的就是内存中的资源,当资源不再被使用的时候,系统会自动回收
			/**
			 * Closes this input stream and releases any system resources associated with
			 * the stream.
			 *
			 * <p>
			 * The <code>close</code> method of <code>InputStream</code> does nothing.
			 *
			 * @exception IOException if an I/O error occurs.
			 */
			// public void close() throws IOException {}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
}

public static void test02() {
	// 目标数组
	String str = "demo";
	byte[] dest = str.getBytes();
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

	try {
		outputStream.write(dest, 0, dest.length);
		System.out.println(new String(outputStream.toByteArray()));
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (outputStream != null) {
				outputStream.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
}

2 处理流:基本数据类型/引用类型输入输出流

2.1 基本数据类型

  • 基本数据类型 + String类型
  • 保留了数据类型和数据(供机器解析和阅读)
    • 输入流:DataInputStream
    • 输出流:DataOutputStream
/*
 * 将数据类型和数据写入字节数组流,需要保证读取的顺序和写入的顺序相同
 */
public static byte[] test01() {
	double d = 2.555;
	long l = 123456789L;
	String s = "哈哈哈哈";
	byte[] b = new byte[100];

	DataOutputStream dos = null;
	ByteArrayOutputStream bos = null;

	try {
		bos = new ByteArrayOutputStream();
		dos = new DataOutputStream(new BufferedOutputStream(bos));
		dos.writeDouble(d);
		dos.writeLong(l);
		dos.writeUTF(s); // 写入UTF-8编码的字符串
		dos.flush();
		b = bos.toByteArray(); // 转换程字节数组
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (bos != null) {
				bos.close();
			}
			if (dos != null) {
				dos.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
	return b;
}

/*
 * 从字节数组流中读出数据类型和数据
 */
public static void test02() {
	byte[] b = test01();
	DataInputStream dis = null;
	try {
		dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(b)));
		System.out.println(dis.readDouble()); // 从基本数据类型流中读取数据
		System.out.println(dis.readLong());
		System.out.println(dis.readUTF());
	} catch (Exception e) {
		e.printStackTrace();
	}
}

2.2 引用类型

  • 引用数据类型
    • 输入流(反序列化):ObjectInputStream
    • 输出流(序列化):ObjectOutputStream

注意
1. 先序列化后反序列化:反序列化顺序必须和序列化顺序相同
2. 不是所有的对象都可以序列化,需要实现java.io.Serializable接口
3. 不是所有属性都需要序列化,不需要序列化的属性增加transient修饰即可

/*
 * 写入引用类型到文件
 */
public static void test01() {
	Employee e1 = new Employee("e1", 1);
	Employee e2 = new Employee("e2", 2);
	Employee e3 = new Employee("e3", 3);
	Employee e4 = new Employee("e4", 4);
	String filePath = "D:\\test\\objectTest.txt";
	File file = new File(filePath);
	ObjectOutputStream outputStream = null;
	try {
		outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
		outputStream.writeObject(e2);
		outputStream.writeObject(e4);
		outputStream.writeObject(e3);
		outputStream.writeObject(e1);
		outputStream.flush();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (outputStream != null) {
				outputStream.close();
			}
		} catch (Exception e5) {
			e5.printStackTrace();
		}
	}
}

/*
 * 从文件中读取引用类型对象
 */
public static void test02() {
	String filePath = "D:\\test\\objectTest.txt";
	File file = new File(filePath);
	ObjectInputStream inputStream = null;
	try {
		inputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
		Employee e1 = (Employee)inputStream.readObject();
		Employee e2 = (Employee)inputStream.readObject();
		Employee e3 = (Employee)inputStream.readObject();
		Employee e4 = (Employee)inputStream.readObject();
		System.out.println(e1.toString());
		System.out.println(e2.toString());
		System.out.println(e3.toString());
		System.out.println(e4.toString());
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (inputStream != null) {
				inputStream.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3 打印流的使用

/*
 * 标准输出流
 */
public static void test01() {
	// 控制台输出
	System.out.println("Hello World");

	// 向文件中输出
	String filePath = "D:\\test\\standardTest.txt";
	PrintStream printStream = null;
	File file = new File(filePath);
	try {
		printStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
		printStream.print(false);
		printStream.println();
		printStream.flush();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
}

/*
 * System的三个常量 System.out;System.err --> 输出流 System.in --> 输入流
 */
public static void test02() {
	// 输出流,在IDE中运行时,err会变红字
	System.out.println("out");
	System.err.println("err");

	// 输入流
	Scanner scanner = new Scanner(System.in);
	System.out.println(scanner.nextLine());
	scanner.close();
}

/*
 * 标准输入流
 */
public static void test03() {
	// 从文件中进行标准输入
	String filePath = "D:\\test\\readTest.txt";
	File file = new File(filePath);
	Scanner scanner = null;
	try {
		scanner = new Scanner(new BufferedInputStream(new FileInputStream(file)), "UTF-8");
		while (scanner.hasNext()) {
			System.out.println(scanner.nextLine());
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

/*
 * 打印位置重定向
 */
public static void test04() {
	// 将System.out重定向到文件中,默认是定向到控制台
	String filePath = "D:\\test\\standardTest.txt";
	File file = new File(filePath);
	try {
		// PrintStream的一个构造函数,第二个参数表明是否进行自动刷新,如下为true(自动刷新缓冲区)
		System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(file)), true));
		// 此时打印的内容会出现在standardTest.txt文件中
		System.out.println("PrintStream_File");
		System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true));
		System.out.println("PrintStream_Console");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
}

/*
 * 封装Scanner的控制台输入功能
 */
public static void test05() {
	InputStream inputStream = System.in;
	// 因为BufferedReader中有readLine方法,方便处理成串的输入内容
	// 因此要使用InputStreamReader转换流对流进行转换
	BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
	try {
		String message = bf.readLine();
		System.out.println(message);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

4 类关系的分类

  • 依赖:一个类是另外一个类的形参或局部变量
  • 关联:一个类是另外一个类的属性
    • 聚合(另一个类的属性):两个类的生命周期不一致
    • 组合(另一个类的属性):两个类的生命周期一致
  • 继承:父子类的关系
  • 实现:接口与实现类的关系

5 IO总结

IO总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值