Catherine_zhilin

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 import java.io.*;
 2 
 3 public class main {
 4     private static String fileName = null;
 5     public static  void main(String[] args){
 6         fileName = "tmp -" + System.currentTimeMillis() + ".txt";
 7         while(true){
 8             writeString("xiao", fileName);
 9             writeString("xian", fileName);
10             writeString("nui", fileName);
11 
12             try {
13                 Thread.sleep(1000);
14             } catch (InterruptedException e) {
15                 e.printStackTrace();
16             }
17         }
18 
19     }
20 
21 
22     private static void writeString(String  message, String fileName){
23         FileOutputStream fileOutputStream = null;
24         try {
25             File tmpFile = new File(fileName);
26             if (!tmpFile.exists()){
27                 boolean create = tmpFile.createNewFile();
28             }
29             fileOutputStream = new FileOutputStream(tmpFile, true);
30             fileOutputStream.write(message.getBytes());
31             fileOutputStream.write("\n".getBytes());
32 
33             System.out.println("write - " + message);
34 
35         } catch (IOException e) {
36             e.printStackTrace();
37         }finally {
38             if (fileOutputStream != null) {
39                 try {
40                     fileOutputStream.close();
41                 } catch (IOException e) {
42                     e.printStackTrace();
43                 }
44             }
45         }}
46 
47 }

测试结果:

 

文本输出内容:

 

posted on 2018-08-28 09:29  kkkshiki  阅读(1137)  评论(0编辑  收藏  举报