首页 > 代码库 > OutputStream & InputStream

OutputStream & InputStream

 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         //write15         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         //read29         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