zoukankan      html  css  js  c++  java
  • java随机产生指定大小文件

    文章目录

    原因

    今天在做测试的时候,需要一个5M的文件,然后一开始是直接ctrl C+ctrl V,虽说也很快,但是很傻,虽然你自信的ctrl C的时候真的很帅,但是后面需求变更的时候,你真的很傻.然后我就简单的写了一个工具类,生成指定大小的文件,就是随机数字for遍历然后加起来…测试嘛,随便搞一下,不会很慢.

    代码:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.Random;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.CountDownLatch;
    
    /**
     * @Version 2020-05-07 14:53
     * @Version 1.0
     * @Description FileCreatUtil单纯的测试使用,单位是M
     */
    public class FileCreatUtil {
    
      //随机产生指定大小的文件,纯数字
      public static void createNewFile(String path, int sizeM) throws Exception {
        File file = new File(path);
        int size = sizeM;
        if (!file.exists()) {
          file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
        CountDownLatch countDownLatch = new CountDownLatch(size);
        for (int i = 0; i < size; i++) {
          CompletableFuture<StringBuilder> c = oneM();
          c.whenComplete((x, y) -> {
            if (x != null) {
              try {
                osw.append(x);
                osw.flush();
              } catch (IOException e) {
                e.printStackTrace();
              }
            }
            countDownLatch.countDown();
          });
        }
        countDownLatch.await();
        osw.close();
      }
    
      //随机产生指定大小的String
      public static String createString(int sizeM) throws Exception {
        int size = sizeM;
        StringBuffer stringBuffer = new StringBuffer();
        CountDownLatch countDownLatch = new CountDownLatch(size);
        for (int i = 0; i < size; i++) {
          CompletableFuture<StringBuilder> c = oneM();
          c.whenComplete((x, y) -> {
            if (x != null) {
              stringBuffer.append(x);
            }
            countDownLatch.countDown();
          });
        }
        countDownLatch.await();
        return stringBuffer.toString();
      }
    
      private static CompletableFuture<StringBuilder> oneM() {
        Random r = new Random();
        CompletableFuture<StringBuilder> c = CompletableFuture.supplyAsync(() -> {
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i < 1024 * 1024; i++) {
            sb.append(r.nextInt(10));
          }
          return sb;
        });
        return c;
      }
    
      public static void main(String[] args) throws Exception {
        //几M
        String string = createString(3);
        System.out.println(1);
    //    createNewFile("E:\2.txt", 3);
      }
    
    
    }
    

    一身轻松!..
    建议1G以内,太大,会卡死,因为直接使用的for循环…

    世界上所有的不公平都是由于当事人能力不足造成的.
  • 相关阅读:
    python模块—socket
    mac os系统的快捷键
    教你如何将UIImageView视图中的图片变成圆角
    关于ASP.NET MVC
    iOS 日期格式的转换
    将App通过XCode上传到AppStore 出现这个错误“An error occurred uploading to the iTunes Store”的解决方法
    关于MAC OS下面两个软件的功能改进——Dictionary和Fit 输入法
    分享一下上个星期的香港行程
    【博客园IT新闻】博客园IT新闻 iPhone 客户端发布
    解决Entity Framework Code First 的问题——Model compatibility cannot be checked because the database does not contain model metadata
  • 原文地址:https://www.cnblogs.com/javayida/p/13346755.html
Copyright © 2011-2022 走看看