编程的时候读写文件是一个经常遇到的情况,Java读写文件的方法有很多种,在文档比较小的时候,使用不同的方法似乎没有什么差异,然而当数据规模非常庞大的时候,读写文件的策略就会影响整个程序的性能。
举一个例子,一个工厂在生产机器非常先进管理很到位的情况下,如果原料供应不上或者产品输出渠道很小,工厂的生产效益也不会很高。
读写文件的过程就相当于工厂的原料供应和产品输出,在整个程序中起着非常重要的作用,尤其是数据量很大的时候。

下面看几种读写文件的策略:
读文件策略:

//stream读文件
try
(Stream<String> stream =
Files.lines(Paths.get(fileName)))
{long start = System.currentTimeMillis();listss = stream.filter(line
-> !line.startsWith("line3")).map(String::toUpperCase.collect(Collectors.toList());long end =System.currentTimeMillis();  System.out.println(end - start + "ms" + listss.size());} catch
(IOException e) {e.printStackTrace();}
File file = new
File("D://hitics/lab5SC", "file1.txt");file.createNewFile();long start2 =
System.currentTimeMillis();try {FileOutputStream in = new FileOutputStream(file);try {int h = 0;for (int i =0; i< listss.size();i++){int len = listss.get(i).length();nt len2 = listss.get(i).getBytes().length;byte bt[] = new byte[len2];bt = (listss.get(i) +"\n").getBytes();in.write(bt);}in.close();long end2 =System.currentTimeMillis();System.out.println(end2 - start2 + "ms");}catch(IOException e) {e.printStackTrace();}
} catch
(FileNotFoundException e) {e.printStackTrace();}
//BufferReaderFileInputStream inputstream = new
FileInputStream(fileName);StringBuffer buffer = new
StringBuffer();
String line;
BufferedReader bufferreader = new
BufferedReader(new
InputStreamReader(inputstream));
line = bufferreader.readLine();
while (line !=null) {buffer.append(line);buffer.append("\n");line = bufferreader.readLine();}inputstream.close();System.out.print(buffer);
//Scanner:
File file = new File(fileName);
String encoding = "utf-8";
List<String> listss2 = new ArrayList<>();if (file.isFile()&& file.exists()){ try {Scanner scanner = newScanner(file, encoding);while/ (scanner.hasNextLine()){listss2.add((scanner.nextLine()).toString());} }
catch (FileNotFoundException e) {e.printStackTrace();}
}

写文件策略:

//FileWritter:
long start1 =System.currentTimeMillis();
String rootString = "D://hitics/lab5SC/";
FileWriter fw = new
FileWriter(rootString + n);for (int i =0; i< s.size();i++)
{w.write(s.get(i) +"\n");
}
long end1 =System.currentTimeMillis();
System.out.println("writeIntoFileByFileWritter" +(end1 - start1) +"ms");
//BufferWritter:
long start2 =
System.currentTimeMillis();
tring rootString = "D://hitics/lab5SC/";try {File f = newFile(rootString + n);f (!f.exists()){f.createNewFile();}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f));
BufferedWriter writer = new BufferedWriter(write);
for (int i =0; i< s.size();i++){writer.write(s.get(i) +"\n");
}writer.flush();
write.close();
riter.close();
} catch
(Exception e) {e.printStackTrace();}
long end2 =System.currentTimeMillis();
System.out.println("writeIntoFileByBufferWritter" +(end2 - start2) +"ms");
//Stream:long start3 =System.currentTimeMillis();
String rootString = "D://hitics/lab5SC/";
File file = new File(rootString + n);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {if (!file.exists()){file.createNewFile();}
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
for (int i =0; i< s.size();i++){osw.write(s.get(i) +"\n");
}
}
catch(IOException e) {e.printStackTrace();
}
finally {if (null !=osw) {
try {
osw.close();
}
catch(IOException e) {
e.printStackTrace();
}}
if (null !=fos) {
try {
fos.close();
}
catch(IOException e) {
e.printStackTrace();
}}}long end3 = System.currentTimeMillis();System.out.println("writeIntoFileByStream" +(end3 - start3) +"ms");

三种读文件策略效率对比(3138005行):

method time
Stream 2000ms
BufferReader 671ms
Scanner 2893ms

三种写文件策略效率对比(8517425行):

method time
BufferWriter 1782ms
FileWritter 3770ms
Stream 2452ms

以上实验的数据可以在具体程序编写的过程中作为参考。

Java多种读写文件文件方法之对比分析相关推荐

  1. java如何读写json文件

    java如何读写json文件 在实际项目开发中,有时会遇到一些全局的配置缓存,最好的做法是配置redis数据库作为数据缓存,而当未有配置redis服务器时,读取静态资源文件(如xml.json等)也是 ...

  2. java怎样读txt文件_【后端开辟】java怎样读写txt文件?

    java怎样读取txt文件? 1.运用FileInputStream完成读取txt文件内容 2.运用FileOutputStream完成写入txt文件内容 package cn.xiaobing.ut ...

  3. Java多种方式读文件,追加文件内容,等对文件的各种操作

    一.多种方式读文件内容.1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 import java.io.BufferedReader;import java. ...

  4. java dom读写xml文件_java通过dom读写xml文件

    java通过dom读写xml文件 要读的xml文件 encoding="GB2312"?> "男"> 李华姓名> 14年龄> 学生> ...

  5. 数据分析方法01对比分析法

    对比分析法 1.概念 基于相同的数据标准下,把两个及以上相互联系的指标数据进行比较,准确量化的分析他们的差异,说明研究对象在规模大小,水平高低,速度快慢等的不同表现,目的是为了找到差异的原因,从而找到 ...

  6. java spark读写hdfs文件,Spark1.4从HDFS读取文件运行Java语言WordCounts

    Hadoop:2.4.0 Spark:1.4.0 Ubuntu 14.0 1.首先启动Hadoop的HDFS系统.     HADOOP_HOME/sbin/start-dfs.sh 2.在Linux ...

  7. java平台读写dxf文件

    想通过dxf文件生成通风网络拓扑图, 第一个问题是dxf文件的读取, 基于java的平台中寻入dxf文件的关键技巧 TWaver导入导出AutoCAD DXF图纸中提到了方法,但是效果不好, http ...

  8. java sax读写xml文件_使用SAXReader读取xml文件

    搜索热词 原XML文件: 99999 5275 0 001 9999 20151221 018888 0100010 5275 6600 99898989 000 c1aaaax QD00112210 ...

  9. java多线程读写excel文件

    使用两个线程,一个线程读,一个线程写,实际上就是一个典型的生产者-消费者模型 public class Application {private String prefix = "G:/JA ...

最新文章

  1. python 去除str的引号 去除括号_Python基础教程:运算符以及数据类型解析
  2. 人工智能和机器人将成为数据中心最佳“伴侣”
  3. error: declaration of 'cv::Mat R ' shadows a parameter
  4. zoj3882 博弈
  5. dqn在训练过程中loss越来越大_[动手学强化学习] 2.DQN解决CartPole-v0问题
  6. 揭秘阿里云 RTS SDK 如何实现直播降低延迟和卡顿
  7. Nginx ab压力测试
  8. 永不示弱_永不过时的网页设计:今天和2000年的在线投资组合
  9. 【牛客 - 157D】插排树(dfs,树形dp)
  10. Python 20 秒画完小猪佩奇“社会人”!
  11. python3.0下载用什么浏览器_无法让Python下载网页源代码:“不支持浏览器版本”...
  12. echart 设置y轴间隔_ECharts 柱状图之间的间距怎么调整的?y轴的线怎么调出来? 做成表格状。...
  13. 三角学——极坐标_2
  14. [SQL SERVER] 优化SQL Server 2000的设置
  15. ini_set() 函数的使用 以及 post_max_size,upload_max_filesize的修改方法
  16. 解决: Windows下启动Redis失败
  17. 【cl】本地安装maven的jar包报错Artifact is already in the local repository
  18. http请求出现406错误解决方案
  19. 基于Java+Springboot+mybatis+lyaui的学科竞赛管理系统设计和实现
  20. logistic回归分析优点_SPSS统计分析全解析Logistic回归(逻辑回归)

热门文章

  1. Blender 物理属性 (二)布料和碰撞
  2. Android仿余额宝实现七天年化收益率图表
  3. CSS用flex布局两端对齐,列不满左对齐
  4. 【C/C++】用格雷戈里公式求π
  5. 什么是无监督、监督、半监督学习
  6. 安全断路器市场现状及未来发展趋势分析
  7. kubectl describe
  8. ubuntu下安装telnet服务
  9. 定时停用计算机服务器,如何让服务器每天定时开关机?
  10. JAVA基础 :数据类型