zoukankan      html  css  js  c++  java
  • java文件生成

    package com.gcy.test.util;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Random;
    
    import org.apache.commons.io.output.FileWriterWithEncoding;
    import org.junit.Test;
    
    public class TestGroupGenerator {
        
        private String TARGET_FILENAME = "group_data";
        private String TARGET_PATH = "/Users/test/" + TARGET_FILENAME;
        Integer[] groupIds = {16,18,19,20,26,27};
        String[] name = {"三坊七巷一店","温泉一店","温泉二店","新港一店","温泉三店","三坊七巷二店"};
        Integer[] deviceCounts = {12,1,1,6,1,5};
        Integer[] onlineCounts = {3,0,4,0,1,1};
        
        @Test
        public void generateFile() {
            //如果已经存在相同文件则删除
            File targetFile = new File(TARGET_PATH);
            targetFile.delete();
        
            try {
                writtenFile(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
      //写文件
          void writtenFile(File file) throws IOException {
              //写文件
              file.createNewFile();
              FileWriterWithEncoding fw = new FileWriterWithEncoding(file, "utf-8", true);
              BufferedWriter bw=new BufferedWriter(fw);
              
              try {
                  for(int i = 0; i < deviceCounts.length; i ++) {
                      String onString = "'" + groupIds[i] + "'" + ":{deviceCount:" + deviceCounts[i] + "," + 
                                      "onlineCount:" + onlineCounts[0] + "," + 
                                      "staCount:" + getInt(100) + "," + 
                                      "upFlow:" + getInt(10000) + "," + 
                                      "downFlow:" + getInt(10000) + "," + 
                                      "gateway:" + getIp() + "," + 
                                      "dns:" + getIp() + "," + 
                                      "name:" + getString(name[i]) + 
                                      "}";
                      bw.write(onString+"
    ");
                  }
                  bw.flush();
                  fw.flush();
                  System.out.println("done");
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } finally {
                  bw.close();
                  fw.close();
              }
              
          }
          
          private String getString(String ob) {
              return "'" + ob + "'";
          }
          
          private int getInt(int i) {
              Random rand = new Random();
              return rand.nextInt(i);
          }
          
          private String getIp() {
              String ip = getInt(255) + "." + getInt(255) + "." + getInt(255) + "." + getInt(255);
              return getString(ip);
          }
    }
  • 相关阅读:
    Ubuntu 虚拟机安装几点细节整理
    jQuery与IE兼容性问题处理
    Excel已损坏,无法打开
    应对刷新闪烁问题
    ArcGIS鼠标滚轮方向之代码篇
    ChartControl控件0和null的效果
    多个组件联合打印输出——PrintableComponentLink
    Skyline中加载WMTS地图
    访问天地图WMTS服务的正确姿势
    超图不支持JPEG格式的WMTS服务
  • 原文地址:https://www.cnblogs.com/guochunyi/p/5284664.html
Copyright © 2011-2022 走看看