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 }
测试结果:
文本输出内容:
转载于:https://www.cnblogs.com/Catherinezhilin/p/9546131.html
相关资源:JAVA上百实例源码以及开源项目