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 }
时间: 2024-10-12 20:55:22