1 package javastudy;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.Calendar;
8
9 public class Txtio {
10 public static void main(String[] args ) throws IOException
11 {
12 /**
13 * 功能:显示一个文件的信息。
14 * 步骤:
15 * 1.加载该文件的具体路径
16 * 2.通过StringBulider来实现具体的信息内容
17 * 2.直接输出显示信息。
18 * 3.通过filereader来输出文件内容
19 * 4,先判断文档内容,是否存在,如果处在就打印出来
20 *
21 * */
22 /* File f= new File("f:\\data.txt");
23 //方法1.建立StringBulider
24 StringBuilder sb = new StringBuilder();
25 Calendar cl = Calendar.getInstance();
26 long time = f.lastModified();
27 cl.setTimeInMillis(time);
28 sb.append("最后修改时间为:"+cl.getTime().toLocaleString());
29 // Calendar cal = Calendar.getInstance();
30 // long time = f.lastModified();
31 // cal.setTimeInMillis(time);
32 // System.out.print("最后修改日期:"+cal.getTime().toLocaleString());
33 sb.append("\n");
34 sb.append("文档名称为:"+f.getName());
35 System.out.println(sb);
36 //判断是否是文件
37 if(f.isDirectory())
38 {
39 System.out.println("是目录");
40 }
41 else
42 {
43 System.out.println("是文件");
44 }
45 try {
46 FileReader fr =new FileReader("f:\\data.txt");
47 //判断文件是否为空
48 int ch =0;
49 System.out.println("文件具体内容为:");
50 while((ch=fr.read())!=-1)
51 {
52 //进行强制转换
53 System.out.print((char)ch);
54 }
55 } catch (FileNotFoundException e) {
56 // TODO Auto-generated catch block
57 System.out.println("文件加载失败!!!");
58 e.printStackTrace();
59 }*/
60 /**
61 * 功能:读取某盘下下的所有文件和目录,并将其打印出来
62 * 步骤:
63 * 1.将需要读取的磁盘加载出来。
64 * 2.因为有很多文件,所以需要先将文件储存某个数组中
65 * 3.判断是否为文件
66 * 4.并分别遍历,及读取到控制台上
67 *
68 * */
69 /* File file =new File("d:\\");
70 //将file的内容储存到数组中。
71 File[] f = file.listFiles();
72 for(File files :f)
73 {
74 if(files.isDirectory())
75 {
76 System.out.print("目录文档:");
77 }
78 else
79 {
80 System.out.print("文件文档:");
81 }
82 System.out.println(files.getPath());
83 }*/
84 /**
85 * 功能:读取某盘下的所有txt文件。
86 * 步骤:
87 * 1.添加一个show类,并加载需要读取的磁盘
88 * 2.将所有数据储存到数组中
89 * 3.进行数组的遍历
90 * 4.判断是否为文件,如果是进行打印
91 * 5.如果是目录进行下次读取
92 * 6.注:在读取文件的过程中可能会遇到磁盘文件不可读取的部分,这是进行判断并返回。
93 *
94 * */
95 String path="e:\\";
96 Show(path);
97 }
98 static void Show(String path)
99 {
100 File file =new File(path);
101 //将file的内容储存到数组中。
102 File[] f = file.listFiles();
103 for(File fi : f)
104 {
105 // System.out.println("----------"+fi.getPath()+"------");
106 if(fi.getPath().endsWith("System Volume Information"))
107 {
108 return;
109 }
110 //以上为判断不能读取的部分
111 if(fi.isFile())
112 {
113 //判断所有目录中的后缀大些为TXT.
114 if(fi.getPath().toUpperCase().endsWith(".TXT"))
115 {
116 System.out.println(fi.getPath());
117 }
118
119 }
120 else
121 {
122 //如果是目录则重新读取:
123 Show(fi.getPath());
124 }
125 }
126 }
127 }
1 package javastudy;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6
7 public class Textio {
8 public static void main(String[] args)
9 {
10 try {
11 FileInputStream fi =new FileInputStream("f:\\data.txt");//获取文件的绝对路径
12 byte[] by=new byte[fi.available()];//新建一个字节数组
13 fi.read(by);//将文件以字节数组的形式保存起来
14 String st=new String(by);//将字节数组转换成字符形式输出
15 System.out.print(st);
16 fi.close();
17 } catch (FileNotFoundException e) {
18 // TODO Auto-generated catch block
19 e.printStackTrace();
20 } catch (IOException e) {
21 // TODO Auto-generated catch block
22 e.printStackTrace();
23 }
24 }
25 }