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 读写文件常用方法的更多相关文章

  1. Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

  2. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  3. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  4. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  5. 【java】java 读写文件

    场景:JDK8  将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...

  6. 转:Java读写文件各种方法及性能比较

    干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...

  7. 161012、JAVA读写文件,如何避免中文乱码

    1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...

  8. java 读写文件乱码问题

    这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...

  9. java读写文件小心缓存数组

    一般我们读写文件的时候都是这么写的,看着没问题哈.   public static void main(String[] args) throws Exception {   FileInputStr ...

随机推荐

  1. C#接收xmlrpc接口返回哈希表格式

    C#在调用xmlrpc接口时返回的是int值就可以直接获取,最近在调用一个接口是获取一个账号记录的详细信息,xmlrpc接口返回的是一个哈希值. 所以直接用int或者Hashtable 来获取返回值执 ...

  2. ab并发负载压力测试

    一.ab 0.安装ab压力测试软件 [root@a2 conf]# yum install httpd-tools -y #查看版本 [root@a2 conf]# ab -V This is Apa ...

  3. c++ opencv3.4 保存和读取PCA模型

    cv::PCA pca(samples, cv::Mat(), cv::PCA::DATA_AS_ROW,); FileStorage fs("pca.xml",FileStora ...

  4. CentOS6.6服务器系统配置(LAMP+phpMyAdmin)全流程

    CentOS6.6服务器系统配置(LAMP+phpMyAdmin)全流程 昨天在腾讯云上买了个服务器,是CentOS6.6操作系统的裸机,里面什么都没,然后开始了一天一夜的LAMP(Apache+My ...

  5. Spring框架学习(5)spring整合struts2

    内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...

  6. Android-ImageView的属性android:scaleType作用

    在网上查了好多资料,大致都雷同,大家都是互相抄袭的,看着很费劲,不好理解,自己总结一下,留着需要看的话来查找. 代码中的例子如下: <ImageView android:id="@+i ...

  7. WPF一段代码搞定所有的点击事件

    用一个方法来控制转发所有的点击事件,界面图如下:    <Page x:Class="WPFNavigate.Index" xmlns="http://schema ...

  8. Linux命令之编辑

    vi是终端命令行里功能最强的文本编辑器了,但眼下须要用到的仅仅是文本编辑功能.与GCC.make等工具的整合应用如今还不须要,所以操作难度不大,习惯就好. Linux发行版所带的一般不是vi,而是vi ...

  9. ssm整合(Spring+SpringMVC+Mybatis)

    一.Spring Spring致力于提供一种方法管理你的业务对象.IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用 ...

  10. JedisCluster操作redis集群demo

    package com.chenk; import java.util.HashMap; import java.util.HashSet; import java.util.List; import ...