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. java 利用JAX-RS快速开发RESTful 服务实例

    首先看web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...

  2. apache2.4 的安装

    Apache2.4 安装包下载地址 http://httpd.apache.org/docs/current/platform/windows.html#down 选择ApacheHaus 进入后 这 ...

  3. 转: 加快Android编译速度

    转: http://timeszoro.xyz/2015/11/25/%E5%8A%A0%E5%BF%ABandroid%E7%BC%96%E8%AF%91%E9%80%9F%E5%BA%A6/ 加快 ...

  4. tomcat中server.xml配置详解(转载)(二)

    转载自:https://www.cnblogs.com/starhu/p/5599773.html 一:<Connector>元素 由Connector接口定义.<Connector ...

  5. vue - vue-loader.conf

    'use strict' // 工具=> build/util.js const utils = require('./utils') // 配置=> build/config/index ...

  6. 使用Loadrunner进行文件的上传和下载

    最近使用loadrunner中需要录制文件的上传和下载,上传功能模块利用录制可以直接实现,下载无法实现,在网上找到了一段代码,自己动手试验了下,发现没有用 辛苦找到的,还是记录下吧 (1)LoadRu ...

  7. recess----1.第一个APP-helloRecess

    选择recess的理由很简单,这个架构看起来很轻量级,很简单.至少是写起应用来感觉不需要考虑太多和架构相关的东西.没有按作者给的过程一步步来,折腾了好久...在这里记录一下. 安装过程略,官网文档无压 ...

  8. STL源码剖析(list)

    SGI STL中list是使用环状双向链表实现的.它的结点结构定义如下: template <class T> struct __list_node { typedef void* voi ...

  9. win10下Visual Studio 2015,C++ x64编译zmq

    PS.本人编译过程踩得坑,记录备忘 下载:(1)官网:http://zeromq.org/intro:get-the-software,有简明的编译方式,cmake的,这里不多赘述 (2)到GitHu ...

  10. Ubuntu下安装JDK7(附Clojure下载)

    转:http://www.linuxidc.com/Linux/2012-10/71557.htm 首先确实不得不说,网上有很多类似Ubuntu下安装JDK7的教程.不过大都是基于JDK6的bin文件 ...