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

package files;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

public class TestIOStreamFilter {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		testDataStream();
		testBufferedStream();
		testLineNumberInputStream();
		testPrintStream();

	}

	public static void testDataStream() throws IOException {
		FileOutputStream fos = new FileOutputStream("./output.txt");
		DataOutputStream dos = new DataOutputStream(fos);
		dos.writeUTF("正则表达式");
		dos.writeInt(1);
		dos.writeDouble(1.11);
		dos.writeChar('a');
		dos.close();
		fos.close();

		FileInputStream fis = new FileInputStream("./output.txt");
		DataInputStream dis = new DataInputStream(fis);

		System.out.println(dis.readUTF());
		System.out.println(dis.readInt());
		System.out.println(dis.readDouble());
		System.out.println(dis.readChar());
		dis.close();
		fis.close();

		fis = new FileInputStream("./input.txt");
		dis = new DataInputStream(fis);
		byte[] bytes = new byte[100];
		dis.read(bytes);
		System.out.println(Charset.forName("UTF-8")
				.decode(ByteBuffer.wrap(bytes)).toString());
		dis.close();
		fis.close();
	}

	public static void testBufferedStream() throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedOutputStream bos = new BufferedOutputStream(baos);
		bos.write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
		bos.flush();
		bos.write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
		bos.flush();

		byte[] bytes = new byte[100];
		BufferedInputStream bis = new BufferedInputStream(
				new ByteArrayInputStream(baos.toByteArray()));
		bis.read(bytes);
		System.out.println(Arrays.toString(bytes));
		bos.close();
		baos.close();
		bis.close();
	}

	public static void testLineNumberInputStream() throws IOException {
		LineNumberInputStream lnis = new LineNumberInputStream(
				new FileInputStream("./input.txt"));
		byte[] bytes = new byte[100];
		System.out.println(lnis.getLineNumber());
		System.out.println(lnis.read(bytes, 0, 1));
		System.out.println(lnis.getLineNumber());
		lnis.setLineNumber(2);
		System.out.println(lnis.read(bytes, 0, 1));
		System.out.println(lnis.getLineNumber());
		lnis.close();
	}

	public static void testPrintStream() throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(baos);
		ps.printf("%s%d", "number: ", 1);
		System.out.println(Arrays.toString(baos.toByteArray()));
		ps.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值