• java 读写文件常用方法


    package study.bigdata;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.lang3.RandomStringUtils;
    import org.junit.Test;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.List;
    import java.util.Random;
    import java.util.UUID;
    
    /**
     * <dependencies>
     <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     </dependency>
     <dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     </dependency>
     <dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-lang3</artifactId>
     </dependency>
     </dependencies>
     */
    public class App {
    
        /**
         * 一行一行地读取文件的例子
         *
         * @throws IOException
         */
        @Test
        public void fileUtilsreadLinesTest() throws IOException {
            List<String> lines = FileUtils.readLines(new File("D:\___WORK\workSpaceHome\temp\study3\toolSet\src\main\java\resource\test.dat"), "UTF-8");
            System.out.println(lines);
        }
    
        /**
         * 将String写入文件的方法
         *
         * @throws IOException
         */
        @Test
        public void fileUtilswriteStringToFile() throws IOException {
            File file = new File("D:\test\toolSet\fileutils\output\out2.dat");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 3000000; i++) {
                sb.append(System.currentTimeMillis()).append(" ").append(UUID.randomUUID()).append("
    ");
            }
            org.apache.commons.io.FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
        }
    
        /**
         * 生成随机数字和字符串
         */
        @Test
        public void randomStringUtilsrandom() {
            System.out.println(RandomStringUtils.randomAlphabetic(4));
            System.out.println(RandomStringUtils.random(5));//产生5位长度的随机字符串,有乱码
            //使用指定的字符生成5位长度的随机字符串
            System.out.println(RandomStringUtils.random(5, new char[]{'a', 'b', 'c', 'd', 'e', 'f'}));
            //生成指定长度的字母和数字的随机组合字符串
            System.out.println(RandomStringUtils.randomAlphanumeric(5));
            System.out.println(RandomStringUtils.randomAlphabetic(5));
            //生成随机数字字符串
            System.out.println(RandomStringUtils.randomNumeric(5));
        }
    
        @Test
        public void writeFile() throws IOException {
            File file = new File("D:\test\toolSet\fileutils\output\out3.dat");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 2000; i++) {
                sb.append(RandomStringUtils.randomAlphabetic(5)).append(" ")
                        .append(RandomStringUtils.randomAlphabetic(5)).append(" ")
                        .append(RandomStringUtils.randomAlphabetic(4)).append(" ")
                        .append(RandomStringUtils.randomAlphabetic(6)).append("
     ");
            }
            FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
        }
    
        /**
         * 写一个1g的文件
         * @throws IOException
         */
        @Test
        public void test1g() throws IOException {
            FileWriter writer = new FileWriter("D:\test\toolSet\fileutils\output\out4.dat");
            BufferedWriter buffer = new BufferedWriter(writer);
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < 1024; j++) {
                sb.append("1");
            }
            long start = System.currentTimeMillis();
            int max = 1 * 1024  * 1024;//1G
            for (int i = 0; i < max; i++) {
                buffer.write(sb.toString());
            }
            long end = System.currentTimeMillis();
    
            System.out.println(end-start);
    
            buffer.flush();
            buffer.close();
            writer.close();
        }
    }
  • 相关阅读:
    买书求如何获得折扣使价格最低
    团队开发NABC特点
    《梦断代码》读后感3
    结对开发5
    找水王问题续
    对搜狗输入法的评价
    团队项目之典型用户
    找水王问题
    电梯调度练习
    团队项目用户需求调研报告
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/7118757.html
Copyright © 2020-2023  润新知