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. 【文件监控】之一:理解 ReadDirectoryChangesW part1

    理解 ReadDirectoryChangesW 原作者:Jim Beveridge 原文:http://qualapps.blogspot.com/2010/05/understanding-rea ...

  2. 摄像头模组 PDAF对焦(Phase Detection Auto Focus)

    本文主要是最近看的两个文档的总结,相对零散的笔记,包括<imx298 software reference PDAF>与<PDAF Truly>. 1.PDAF功能的实现需要使 ...

  3. 用sencha touch的Cmd创建的MVC工程需要注意的问题

    用ST的cmd创建的js文件都是ANSI编码格式的,所以导致无法正常显示中文.例如传输的参数为中文时就为乱码,导致各种问题... 解决办法:将js文件用记事本打开,另存为,选择编码为UTF-8,覆盖原 ...

  4. Android中为你的应用程序添加桌面快捷方式

    public void ShortCut(View view){ createDeskShortCut(this,getString(R.string.short_cut),R.drawable.up ...

  5. java 合并排序算法、冒泡排序算法、选择排序算法、插入排序算法、快速排序算法的描述

    算法是在有限步骤内求解某一问题所使用的一组定义明确的规则.通俗点说,就是计算机解题的过程.在这个过程中,无论是形成解题思路还是编写程序,都是在实施某种算法.前者是推理实现的算法,后者是操作实现的算法. ...

  6. lua coroutine

    Lua中协程都放在表coroutine中. Lua协程的四个状态 挂起(suspended):一个协程被创建的时候,处于挂起状态,不会自动运行. 运行(running):coroutine.resum ...

  7. Win7下Python WEB环境搭建

    环境介绍: Win7 64位 SP1 Python:2.7.6 网关接口:flup Nginx安装:http://blog.csdn.net/jacson_bai/article/details/46 ...

  8. 改造的unity3d文件打包脚本

    ExportAssetBundles.rar // C# Example // Builds an asset bundle from the selected objects in the proj ...

  9. vue 项目心得

    v-bind 数据绑定 可以简写成 : 通过 props 将数据对象 传给 组件 export default { props: { seller: { type: Object } } } crea ...

  10. 2、jQuery选择器

    2.1 基本选择器 它有元素Id.Class.元素名.多个选择符组成,通过基本选择器可以实现大多数页面元素的查找. 选择器 功能 返回值 #id 根据给定的ID匹配一个元素 单个元素 element ...