IO流应用:读取一行数据PrintRowData()、字节流转换为字符流TransStream()、键盘录入最常见写法TransStream2()、键盘录入保存到文件TS_KeyToFile()、获取异常日志信息GetExceptionInfo()、管道流PipedStream()、

 1 //读取一行数据当输入jean时就停止输入退出
 2     public static void PrintRowData()throws IOException
 3     {
 4         InputStream ins =System.in;
 5         StringBuffer sb =new StringBuffer();
 6         
 7         
 8         while(true)
 9         {
10             int flag =ins.read();
11             if (flag=='\r')
12                 continue;
13             if (flag=='\n')
14             {
15                 String s =sb.toString();
16                 if ("jean".equals(s))
17                     break;
18                 sop(s.toUpperCase());
19                 sb.delete(0,s.length());
20                 //sb.deleter(0,sb.length());
21             }
22             else sb.append((char)flag);
23             //sop(sb);
24         }
25         
26     }
PrintRowData()
 1 //将字节流转换为字符流,就可以使用字符流缓冲区中的readLine方法
 2     public static void TransStream()throws IOException
 3     {
 4         /*InputStream ins =System.in;
 5 
 6         //转换流InputStreamReader
 7         InputStreamReader insr= new InputStreamReader(ins);
 8 
 9         //提高效率,使用缓冲区技术
10         BufferedReader bufr =new BufferedReader(insr); */
11         
12         //3-->1
13         BufferedReader bufr =new BufferedReader(new InputStreamReader(System.in));
14         String line=null;
15         while((line=bufr.readLine())!=null)
16         {
17             if ("jean".equals(line))
18                 break;
19             sop(line.toUpperCase());
20         }
21         bufr.close();
22     }
TransStream()
 1 //键盘录入最常见写法:
 2     public static void TransStream2()throws IOException
 3     {
 4         BufferedReader bufr =new BufferedReader(new InputStreamReader(System.in));
 5         BufferedWriter bufw =new BufferedWriter(new OutputStreamWriter(System.out));
 6 
 7         String line =null;
 8         while((line=bufr.readLine())!=null)
 9         {
10             if ("over".equals(line))
11                 break;
12             bufw.write(line.toUpperCase());
13             bufw.newLine();
14             bufw.flush();
15         }
16     }
TransStream2()
 1 //键盘录入的数据存储到一个文件中
 2     public static void TS_KeyToFile()
 3     {
 4         BufferedReader bufr =null;
 5         BufferedWriter bufw =null;
 6 
 7         String line =null;
 8         try
 9         {
10             bufr =new BufferedReader(new InputStreamReader(System.in));
11             bufw =new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo.log")));
12             while((line=bufr.readLine())!=null)
13             {
14                 if ("end".equals(line))
15                     break;
16                 bufw.write(line);
17                 bufw.newLine();
18                 bufw.flush();
19 
20             }
21         }
22         catch (IOException e)
23         {
24             throw new RuntimeException("存储失败");
25         }
26         finally
27         {
28             try
29             {
30                 if (bufr!=null)
31                 {
32                     bufr.close();
33                 }
34             }
35             catch (IOException e)
36             {
37                 throw new RuntimeException("源读取失败");
38             }
39             try
40             {
41                 if (bufw!=null)
42                 {
43                     bufw.close();
44                 }
45             }
46             catch (IOException e)
47             {
48                 throw new RuntimeException("文件写入失败");
49             }
50         }
51         
52     }
TS_KeyToFile()
 1 //获取异常日志信息
 2     public static void getExceptionInfo()
 3     {
 4         
 5         try
 6         {
 7             byte [] buf =new byte[2];
 8             sop(buf[2]);    
 9         }
10         catch (Exception e)
11         {
12             try
13             {
14                 Date d =new Date();
15                 SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
16                 String s =sdf.format(d);
17 
18                 PrintStream ps =new PrintStream("exceptionInfo.log");
19                 ps.println(s);
20                 System.setOut(ps);//改变标准输出设备
21             }
22             catch (IOException ex)
23             {
24                 throw new RuntimeException("日志文件创建失败");
25             }
26             e.printStackTrace(System.out);//记录错误信息
27         }
28     }
GetExceptionInfo()
 1 import java.io.*;
 2 
 3 
 4 class Read implements Runnable
 5 {
 6     private PipedInputStream in;
 7     Read(PipedInputStream in)
 8     {
 9         this.in =in;
10     }
11 
12     public void run()
13     {
14         try
15         {
16             byte [] buf =new byte[1024];
17 
18             System.out.println("读取数据前:没有数据,现在是阻塞状态");
19             int len=in.read(buf);
20 
21             System.out.println("读取到了数据,数据来了,阻塞结束");
22             
23             String s =new String(buf,0,len);
24             System.out.println(s);
25             in.close();
26         }
27         catch (IOException e)
28         {
29             throw new RuntimeException("管道读取流失败");
30         }
31     }
32 }
33 
34 class Write implements Runnable
35 {
36     private PipedOutputStream out;
37     Write(PipedOutputStream out)
38     {
39         this.out =out;
40     }
41     public void run()
42     {
43         try
44         {
45             
46             System.out.println("开始写入数据了,等待3s后开始:");
47             Thread.sleep(3000);
48             
49             out.write("i love you".getBytes());
50             out.close();
51         }
52         catch (Exception e)
53         {
54             throw new RuntimeException("管道写入流失败");
55         }
56     }    
57 
58 }
59 class  PipedStreamDemo
60 {
61     public static void main(String[] args)throws IOException
62     {
63         PipedInputStream in =new PipedInputStream();
64         PipedOutputStream out =new PipedOutputStream();
65 
66         in.connect(out);
67 
68         Write w =new Write(out);
69         Read r =new Read(in);
70 
71         new Thread(r).start();
72         new Thread(w).start();
73         
74     }
75 }
PipedStreamDemo

 

posted @ 2014-04-22 23:59  ITB  阅读(323)  评论(0)    收藏  举报