【java】读写文件

不要吐槽我为啥不写try,catch。

 

默认的相对路径是在工作空间的目录下。

如图

 

 

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class filerw {
 5     public static void main(String []args) throws IOException {
 6         
 7         String pathname = "in.txt";
 8         FileReader reader = new FileReader(pathname);
 9         BufferedReader br = new BufferedReader(reader);
10         String tmpString;
11         while( (tmpString = br.readLine())!= null) {
12             System.out.println(tmpString);
13         }
14         
15         
16         File file = new File("out.txt");
17         
18         FileWriter fw = new FileWriter(file.getName(),true);
19         String data = " This is a test";
20         fw.write(data);
21         fw.close();
22         
23         FileWriter fw1 = new FileWriter(file);
24         BufferedWriter bw = new BufferedWriter(fw1);
25         bw.write(data);
26         bw.close();
27         
28     
29     }
30 }

 

posted @ 2019-03-22 15:53  甜酒果。  阅读(139)  评论(0编辑  收藏  举报