1 import java.io.File;
 2  
 3 public class Scaner {
 4  
 5     public static void main(String[] args) {
 6         printFiles(new File("E:\\practice\\ReadFileProperty"), 1);
 7     }
 8     public static void printFiles(File dir,int tab) {
 9         if(dir.isDirectory()) {
10             File next[]=dir.listFiles();
11             for (int i = 0; i < next.length; i++) {
12                 for (int j = 0; j < tab; j++) {
13                     System.out.print("|--");
14                 }
15                 System.out.println(next[i].getName());
16                 if(next[i].isDirectory()) {
17                     printFiles(next[i], tab+1);
18                 }
19             }
20         }
21     }
22 }

文件的读写操作:

1 import java.io.BufferedReader;
 2 import java.io.BufferedWriter;
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStreamReader;
 9 import java.io.OutputStreamWriter;
10 import java.io.UnsupportedEncodingException;
11  
12 public class readFile {
13  
14     public static void main(String[] args) {
15         File file=new File("test.txt");
16         if(file.exists()) {
17             System.out.println("文件存在");
18              
19             try {
20                 FileInputStream fis=new FileInputStream(file);
21                 InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
22                 BufferedReader br=new BufferedReader(isr);
23                   
24                 String line;
25                 while((line=br.readLine())!=null) {
26                     System.out.println(line);
27                 }
28                 br.close();
29                 isr.close();
30                 fis.close();
31             } catch (FileNotFoundException e) {
32                 e.printStackTrace();
33             } catch (UnsupportedEncodingException e) {
34                 e.printStackTrace();
35             } catch (IOException e) {
36                 e.printStackTrace();
37             }
38         }
39         try {
40             File newFile=new File("newText.txt");
41             FileOutputStream fos=new FileOutputStream(newFile);
42             OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
43             BufferedWriter bw=new BufferedWriter(osw);
44              
45             bw.write("长歌行\n");
46             bw.write("青青园中葵,朝露待日晞。\n");
47             bw.write("阳春布德泽,万物生光辉。\n");
48             bw.write("常恐秋节至,焜黄华叶衰。\n");
49             bw.write("百川东到海,何时复西归? \n");
50             bw.write("少壮不努力,老大徒伤悲。\n");
51              
52             bw.close();
53             osw.close();
54             fos.close();
55              
56             System.out.println("写入完成");
57         } catch (FileNotFoundException e) {
58             e.printStackTrace();
59         } catch (UnsupportedEncodingException e) {
60             e.printStackTrace();
61         } catch (IOException e) {
62             e.printStackTrace();
63         }
64     }
65  
66 }

程序输出文件test.txt中的内容,并且把内容写入到新文件newText.txt中。