常用操作
1 import java.io.File;
2 import java.io.FileReader;
3 import java.io.FileWriter;
4
5 public class FileDemo3 {
6 public static void main(String[] args) throws Exception {
7 // 字符流的操作
8
9 // 字符流的读写类都是集成Reader/Writer
10
11 File file = new File("demo.txt");
12
13 FileWriter fw = new FileWriter(file);
14
15 fw.write("abcdefg"); // 主要是可以写入字符串 比较方便 不需要转成byte[]
16
17 fw.close();
18
19 // 读取
20 FileReader fr = new FileReader(file);
21 StringBuffer strBuffer = new StringBuffer();
22
23 char[] buf = new char[20];
24 int length;
25 while (-1 != (length = fr.read(buf, 0, buf.length))) {
26 strBuffer.append(buf, 0, length);
27 }
28
29 fr.close();
30
31 if(!file.delete())
32 System.out.println("删除文件失败");
33
34 System.out.println(strBuffer.toString());
35 }
36 }
My New Blog : http://blog.fdlife.info/
The more you know, the less you believe.