zoukankan      html  css  js  c++  java
  • 大文本文件读取实验

    大文本文件是用此文的方法生成的。

    读取程序较简单,无需赘述,贴于此地以备考:

    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日--

  • 相关阅读:
    SVN的安装和使用手册
    高德坐标/百度地图获取经纬度
    MVC中返回json数据的两种方式
    [译]Flutter JSON和序列化
    ios 网络字节顺序的转换HTOS
    [swift]可选类型
    向OC类中添加默认的协议实现(ProtocolKit)
    [swift] NSClassFromString 无法获得该类
    Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic
    iOS 9适配技巧
  • 原文地址:https://www.cnblogs.com/heyang78/p/12382779.html
Copyright © 2011-2022 走看看