大文本文件读取实验
大文本文件是用此文的方法生成的。
读取程序较简单,无需赘述,贴于此地以备考:
package readwritefile; import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class BigFileReader { public static void main(String[] args) { long startMs=System.currentTimeMillis(); Path filepath=Paths.get("c:\\temp\\20200229.txt"); Charset u8set=Charset.forName("utf-8"); try(BufferedReader reader=Files.newBufferedReader(filepath, u8set)){ String line=""; int max=-1; do { line=reader.readLine(); try { int num=Integer.parseInt(line); if(num>max) { max=num; } }catch(NumberFormatException ex) { break; } }while(line!=null); System.out.println("The biggest number is "+ max); }catch(IOException ex) { ex.printStackTrace(); } long endMs=System.currentTimeMillis(); System.out.println("File:'"+filepath+"' created,time elapsed:"+ms2DHMS(startMs,endMs)); } /** * change seconds to DayHourMinuteSecond format * * @param startMs * @param endMs * @return */ private static String ms2DHMS(long startMs, long endMs) { String retval = null; long secondCount = (endMs - startMs) / 1000; String ms = (endMs - startMs) % 1000 + "ms"; long days = secondCount / (60 * 60 * 24); long hours = (secondCount % (60 * 60 * 24)) / (60 * 60); long minutes = (secondCount % (60 * 60)) / 60; long seconds = secondCount % 60; if (days > 0) { retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s"; } else if (hours > 0) { retval = hours + "h" + minutes + "m" + seconds + "s"; } else if (minutes > 0) { retval = minutes + "m" + seconds + "s"; } else if(seconds > 0) { retval = seconds + "s"; }else { return ms; } return retval + ms; } }
输出:
The biggest number is 99999991
File:'c:\temp\20200229.txt' created,time elapsed:126ms
--2020年2月29日--
分类:
Java.文本文件操作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2016-02-29 转帖:解决jquery.js在myeclipse中报错的问题
2016-02-29 T450的Fn lock
2016-02-29 【Canvas与艺术】绘制黄色三角辐射警示标志