Thinking in Mobile

学海无涯乐作舟

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 26 Stories :: 1 Comments :: 0 Trackbacks

 

 1//Demo:输出字符串到一个文件而不覆盖原文件的内容(追加)
 2import java.io.*;
 3class WriteToFile 
 4{
 5 public static void main(String[] args) 
 6 {
 7  File f=new File("out.txt");
 8  BufferedWriter out=null;
 9  try
10  {
11   if (!f.exists())
12   {
13    f.createNewFile();//如果out.txt不存在,则创建一个新文件
14   }

15   out = new BufferedWriter(new FileWriter(f,true));//参数true表示将输出追加到文件内容的末尾而不覆盖原来的内容
16   out.write("这是第一行");
17   out.newLine(); //换行
18   out.write("这是第二行");
19   out.newLine();
20  }

21  catch (IOException e)
22  {
23   e.printStackTrace();
24  }

25  finally {
26   try
27   {
28    if (out!=null)
29    {
30     out.close();
31    }

32   }

33   catch (IOException e)
34   {
35    e.printStackTrace();
36   }

37  }

38 }

39}

40

 

posted on 2006-08-30 22:35 W.Y.H 阅读(1861) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: