JAVA的文件创建

 1 package com.xia;
 2 import java.io.*;
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         
 7         //输出流
 8         
 9         try {
10             FileOutputStream fos = new FileOutputStream("d:\\213.txt",true); //true 允许追加写入
11             
12             String str = "\n大家下午好!天气很不错!";  //  \n "转义字符"
13             
14             String str2 = "\n心情不错";
15             
16             fos.write(str.getBytes()); //覆盖写入
17             
18             fos.close();
19             
20             FileInputStream fis = new FileInputStream("d:\\213.txt");
21             
22             byte[] b = new byte[2048];
23             
24             int i = fis.read(b);
25             
26             String str1 = new String(b, 0 ,i);
27             
28             System.out.println("读取内容 = "+ str1);
29             
30             fis.close();
31             
32         } catch (Exception e) {
33             
34             e.printStackTrace();
35         }
36         
37         
38     }
39 
40 }

FileOutputStream fos = new FileOutputStream("d:\\213.txt",true);

fos.write(str.getBytes()); 

byte[] b = new byte[2048];

int i = fis.read(b);

String str1 = new String(b, 0 ,i);

System.out.println("读取内容 = "+ str1);

fis.close();

 1 package com.xia;
 2 
 3 import java.io.*;
 4 
 5 public class test3 {
 6 
 7     public static void main(String[] args) {
 8         
 9         try {
10             
11             FileReader fr = new FileReader("d:\\213.txt");
12             
13             char[] c= new char[2048];
14             
15             int i = fr.read(c);
16             
17             String str = new String (c,0,i);
18             
19             System.out.println("读取内容 = " + str);
20             
21             fr.close();
22             
23         //写入
24             
25             FileWriter fw = new FileWriter("d:\\213.txt", true);
26             
27             fw.write("\n新追加的内容");
28             
29             fw.close();
30             
31         } catch (Exception e) {
32             
33             e.printStackTrace();
34         }
35     }
36 }

 

 

posted @ 2016-03-14 19:37  宫崎天川  阅读(209)  评论(0编辑  收藏  举报