zoukankan      html  css  js  c++  java
  • Java--将数据以文本形式导出

     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("
    ".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 }

    测试结果:

    文本输出内容:

  • 相关阅读:
    《作业二》总结
    《作业一》总结
    团队项目-需求分析报告
    团队项目-选题报告
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    第12组 团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/9546131.html
Copyright © 2011-2022 走看看