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读写文件常用方法
一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...
- Java读写文件方法总结
Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- 【java】java 读写文件
场景:JDK8 将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...
- 转:Java读写文件各种方法及性能比较
干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...
- 161012、JAVA读写文件,如何避免中文乱码
1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...
- java 读写文件乱码问题
这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...
- java读写文件小心缓存数组
一般我们读写文件的时候都是这么写的,看着没问题哈. public static void main(String[] args) throws Exception { FileInputStr ...
随机推荐
- reconstruct-original-digits-from-english(好)
https://leetcode.com/problems/reconstruct-original-digits-from-english/ //https://discuss.leetcode.c ...
- JVM调优思路
一.jvm内存调优 (Gc 和 Full gc) hotspot -Xms40m 最小堆内存 -Xmx512m 最大值内存 -verboose:gc -XX:PrintGCDetails -XX: ...
- Oracle 命令导入数据
1.用命令进入sqlplus: sqlplus 用户名:/orcl 2.执行sql文件 sql>@D:/lanxi_his_data/V_JH_VISITINFO.sql
- oracle 解锁表的一个小问题
最近开发的时候遇到一个小问题,执行一段sql之后发现 表被锁了 显示错误为:ora-00054:resource busy and acquire with nowait specified 百度之后 ...
- AWK 怎么读取标准输入(STDIN)
在 awk 系列中,我们将会看到几个例子,你可以筛选其他命令的输出代替从一个文件读取输入作为 awk 的输入.我们首先从使用 dir 命令开始,它类似于 ls 命令. 在第一个例子下面,我们使用 di ...
- Python strings, 元组tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的
在python中,strings, 元组tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象. a = 1 def fun(a): a = 2 fun(a ...
- Flutter网络请求与JSON解析
本文介绍如何在Flutter中创建HTTP网络请求和对请求的json string进行类型解析. 网络请求 官方使用的是用dart io中的HttpClient发起的请求,但HttpClient本身功 ...
- Unity3d_ADBannerView
原地址:http://blog.csdn.net/cynixway/article/details/7686393 ADBnnerView提供对Apple iAd框架中ADBannerView的包中, ...
- 算法笔记_071:SPFA算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 具体编码 1 问题描述 何为spfa(Shortest Path Faster Algorithm)算法? spfa算法功能:给定一个加权连通图,选取一个 ...
- Simple Factory (简单工厂模式)
简单工厂模式不是23种设计模式之一,简单工厂模式属于创建型模式, 又叫做静态工厂方法(Static Factory Method) 简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模 ...