码迷,mamicode.com
首页 > 其他好文 > 详细

OutputStream & InputStream

时间:2014-08-28 19:44:05      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   java   io   ar   div   log   

 1  2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 import java.util.Scanner;
 9 
10 public class TestFile {
11 
12     public static void main(String[] args) throws Exception {
13         File f=new File("D:"+File.separator+"tt.txt");
14         //write
15         OutputStream out=new FileOutputStream(f,true);
16         Scanner reader=new Scanner(System.in);
17         StringBuffer sb=new StringBuffer();
18         System.out.println("Enter String end with ‘save‘");
19         String s=reader.nextLine();
20         while(!s.equals("save")){
21         
22             sb.append(s+"\r\n");      // \r\n增加换行
23             s=reader.nextLine();
24         }
25         byte[] b=sb.toString().getBytes();
26         out.write(b);
27         
28         //read
29         InputStream in=new FileInputStream(f);
30         byte r[]=new byte[(int) f.length()];
31         int len=in.read(r);
32         System.out.println(new String(r,0,len));
33     }
34 
35 }

 

OutputStream & InputStream

标签:style   blog   color   os   java   io   ar   div   log   

原文地址:http://www.cnblogs.com/thrive/p/3942251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!