java学习笔记之BufferedIO

处理流的类型

缓冲流

看栗子:

 1 package io;
 2 
 3 import java.io.*;
 4 
 5 public class TestBufferStream1 {
 6     public static void main(String[] args) {
 7         try {
 8             FileInputStream fis = new FileInputStream("d:/1.txt");
 9             BufferedInputStream bis = new BufferedInputStream(fis);
10             int c = 0;
11             System.out.println(bis.read());//马士兵老师是这样解释的,咣当读一个,咣当再读一个
12             System.out.println(bis.read());
13             bis.mark(100);//标记到一百个字符
14             for (int i = 0; i <= 10 && (c = bis.read()) != -1; i++) {
15                 System.out.print((char) c + " ");
16             }
17             System.out.println();
18             bis.reset();//回到标记点啦
19             for (int i = 0; i <= 10 && (c = bis.read()) != -1; i++) {
20                 System.out.print((char) c + " ");
21             }
22             bis.close();
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26     }
27 }

疑惑之处:read返回值问题,读8个bit,为什么可以用char转化,mark与reset的用法

那什么字节数组字符数组读啦等等

再举个栗子:

 1 package io;
 2 
 3 import java.io.*;
 4 public class TestBufferStream2 {
 5   public static void main(String[] args) {
 6     try {
 7       BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\share\\java\\dat2.txt"));//直接new(路径)是不行的
 8       BufferedReader br = new BufferedReader(
 9              new FileReader("d:\\share\\java\\dat2.txt"));
10       String s = null;
11       for(int i=1;i<=100;i++){
12         s = String.valueOf(Math.random());
13         bw.write(s);
14         bw.newLine();
15       }
16       bw.flush();
17       while((s=br.readLine())!=null){
18         System.out.println(s);
19       }
20       bw.close(); 
21       br.close();
22     } catch (IOException e) { e.printStackTrace();}
23   }
24 }

 

posted @ 2013-09-12 10:30  董文博  阅读(252)  评论(0编辑  收藏  举报