Java I/O 学习笔记(2) 基本输入输出流

时间:2023-02-15 07:25:54
package files;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.io.StringBufferInputStream;
import java.util.Arrays;
import java.util.Vector;

public class TestIOStream {

	public static void main(String[] args) throws IOException {
		testByteArrayStream();
		testStringBufferInputStream();
		testFileStream();
		testSequenceInputStream();
		testSequenceInputStream1();
	}

	public static void testByteArrayStream() throws IOException {
		byte[] byteArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
		System.out.print("test ByteArrayInputStream: ");
		while (bais.available() != 0) {
			int data = bais.read();
			System.out.print(data + ", ");
		}
		System.out.print("\n");

		System.out.print("test ByteArrayInputStream: ");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		baos.write(new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });
		System.out.print(Arrays.toString(baos.toByteArray()));
		System.out.print("\n");
	}

	public static void testStringBufferInputStream() throws IOException {
		byte[] byteArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		StringBufferInputStream sbis = new StringBufferInputStream(
				"abcdefghijklmn");
		System.out.print("test StringBufferInputStream: ");
		while (sbis.available() != 0) {
			int data = sbis.read();
			System.out.print(data + ", ");
		}
		System.out.print("\n");

		System.out.print("test StringBufferInputStream: ");
		sbis.reset();
		byte[] byteArray1 = new byte[28];
		sbis.read(byteArray1);
		System.out.print(Arrays.toString(byteArray1));
		System.out.print("\n");
	}

	public static void testFileStream() throws IOException {
		FileInputStream fis = new FileInputStream("./input.txt");
		System.out.print("test FileInputStream: ");
		while (fis.available() != 0) {
			int data = fis.read();
			System.out.print(data + ", ");
		}
		fis.close();
		System.out.print("\n");

		fis = new FileInputStream("./input.txt");
		FileOutputStream fos = new FileOutputStream("./output.txt");
		System.out.print("test FileOutputStream: ");
		while (fis.available() != 0) {
			int data = fis.read();
			fos.write(data);
		}
		fis.close();
		fos.close();
		System.out.print("\n");
	}

	public static void testSequenceInputStream() throws IOException {
		FileInputStream fis = new FileInputStream("./input.txt");
		FileInputStream fis1 = new FileInputStream("./input1.txt");
		SequenceInputStream sis = new SequenceInputStream(fis, fis1);
		FileOutputStream fos = new FileOutputStream("./output.txt");
		int temp = 0;
		while ((temp = sis.read()) != -1) {
			fos.write(temp);
		}
		sis.close();
		fis.close();
		fis1.close();
		fos.close();
	}

	public static void testSequenceInputStream1() throws IOException {
		FileInputStream fis = new FileInputStream("./input.txt");
		FileInputStream fis1 = new FileInputStream("./input1.txt");
		Vector<FileInputStream> vec = new Vector<FileInputStream>();
		vec.add(fis);
		vec.add(fis1);
		SequenceInputStream sis = new SequenceInputStream(vec.elements());
		FileOutputStream fos = new FileOutputStream("./output.txt");
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = sis.read(buf)) != -1) {
			fos.write(buf, 0, len);
		}
		sis.close();
		fis.close();
		fis1.close();
		fos.close();
	}

	public static void testPipedStream() {

	}
}