码迷,mamicode.com
首页 > 编程语言 > 详细

java 读写文件常用方法

时间:2017-07-04 22:25:37      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:tools   class   writer   pac   print   写入   fileutils   指定   pid   

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

 

java 读写文件常用方法

标签:tools   class   writer   pac   print   写入   fileutils   指定   pid   

原文地址:http://www.cnblogs.com/rocky-AGE-24/p/7118757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!