缓冲流

             缓冲流:“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率
import java.io.*;

public class TestBufferStream1 {
	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream("d:/HelloWorld.java");
			BufferedInputStream bis = new BufferedInputStream(fis); // 写入缓存区
			int c = 0;
			System.out.println(bis.read());
			System.out.println(bis.read());
			bis.mark(100); // 标记到100,从100开始读
			for (int i = 0; i <= 10 && (c = bis.read()) != -1; i++) {
				System.out.print((char) c + " ");
			}
			System.out.println();
			bis.reset(); // 回到标记100处
			for (int i = 0; i <= 10 && (c = bis.read()) != -1; i++) {
				System.out.print((char) c + " ");
			}
			bis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

import java.io.*;

public class TestBufferStream2 {
	public static void main(String[] args) {
		try { // 带缓冲区的输入流
			BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\dat2.txt"));
			BufferedReader br = new BufferedReader(new FileReader("d:\\dat2.txt"));
			String s = null;
			for (int i = 1; i <= 100; i++) {  //写入100随机数
				s = String.valueOf(Math.random());
				bw.write(s);
				bw.newLine();
			}
			bw.flush();
			while ((s = br.readLine()) != null) {  //每次读一行
				System.out.println(s);
			}
			bw.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,C++,c,C#)