博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java文件生成
阅读量:4692 次
发布时间:2019-06-09

本文共 2573 字,大约阅读时间需要 8 分钟。

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+"\r\n");              }              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);      }}

 

转载于:https://www.cnblogs.com/guochunyi/p/5284664.html

你可能感兴趣的文章
JSP页面间传递参数
查看>>
VSNETcodePrint 2005 & SQL ServerPrint 2005
查看>>
java数组基本操作
查看>>
String的indexOf()用于获取字符串中某个子字符串的位置
查看>>
shell 脚本运算符
查看>>
杭电 1711 Number Sequence
查看>>
又一道软通动力7K月薪面试题——银行业务调度系统
查看>>
Matlab画图-非常具体,非常全面
查看>>
浏览器同源策略及其规避方法
查看>>
ReactJS入门
查看>>
linux网站配置文件.htaccess伪静态转换到IIS web.config中
查看>>
CodeForces 1B
查看>>
win10应用UserControl
查看>>
Magento开发文档(二):Magento配置
查看>>
用递归的方法,判断某个字符串是否为回文
查看>>
[LeetCode] 100. Same Tree Java
查看>>
BZOJ4516: [Sdoi2016]生成魔咒(后缀自动机)
查看>>
查看手机已经记住的WIFI密码
查看>>
Linux实战教学笔记24:SSH连接原理及ssh-key
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>