大文本文件是用此文的方法生成的。
读取程序较简单,无需赘述,贴于此地以备考:
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: emp20200229.txt' created,time elapsed:126ms
--2020年2月29日--