java 读写文件常用方法

Posted 牵牛花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("\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 读写文件常用方法的主要内容,如果未能解决你的问题,请参考以下文章

java 读写文件常用方法

常用python日期日志获取内容循环的代码片段

java高性能文件读写的工具类?

# Java 常用代码片段

# Java 常用代码片段

java ------ I/O 读写文件