1 import java.io.*; 2 3 public class Test3 { 4 5 public static void main(String[] args) { 6 try { 7 PrintWriter out = new PrintWriter( 8 new BufferedWriter( 9 new OutputStreamWriter( 10 new FileOutputStream("a.txt")))); 11 String s = "hello world!"; 12 out.print(s);//给文件录入信息 13 out.close(); 14 BufferedReader in = new BufferedReader( 15 new InputStreamReader( 16 new FileInputStream("a.txt"))); 17 String line; 18 //读取文件中的信息 19 while((line = in.readLine()) != null) 20 { 21 System.out.println(line); 22 } 23 in.close(); 24 } catch (FileNotFoundException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } catch (IOException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 } 32 33 }
今日学习的文件输入输出流,与之前学习的流有挺多相似的,但是类也挺多的,所以容易搞混,需要好好去记住区分区别。
附文本文件截图: