• Java如何实现按指定行读取文件


        最近在开发实战中,遇到了一个这样的技术情景:

        把log4j生成的日志文件定时刷进MySQL数据库,比如三个小时刷一次,那么每次刷数据的时候,如何控制文件读取是从上一次文件读取结束的地方开始继续读取的?并且本次要读取到文件结尾处。在网上各种搜索提问后,找到了一个叫RandomAccessFile Java类解决了问题。

    static int size=1;//主要是为了控制循环的次数,因为是定时刷,每次刷的文件行数可能不一样
        static long chars=0;//chars指的是字符数
        /**
         * 读取文件内容
         * @param fileName
         */
          public Map readANDwrite(String fileName) {
                
                //大集合,以sessionid为键,以一次session的所有访问记录list为值进行存储
                Map<String,Map> bigMap = new HashMap();
                
                //一次session的访问记录集合
                File file = new File(fileName);
                
                //java提供的一个可以分页读取文件的类,此类的实例支持对随机访问文件的读取和写入
                RandomAccessFile rf = null;
                
                String tempString = null;
                try {
                    
                        //初始化RandomAccessFile,参数一个为文件路径,一个为权限设置,这点与Linux类似,r为读,w为写
                    rf = new RandomAccessFile(fileName, "rw");
                    
                    //设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作
                    rf.seek(chars);
                    
                    //获取文件的行数
                    int fileSize = getTotalLines(file);
                        for (int i = size-1; i < fileSize; i++) {//从上一次读取结束时的文件行数到本次读取文件时的总行数中间的这个差数就是循环次数
                        
                        //一行一行读取
                        tempString = rf.readLine();
                        //文件中文乱码处理
                        tempString = tempString.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
                        tempString = tempString.replaceAll("\+", "%2B");
                        tempString = java.net.URLDecoder.decode(tempString, "GB2312");
                    
                        //将字符串JSON转换为实体JSON,以便能通过key取value
                        JSONObject json = JSONObject.fromObject(tempString);
                        String refPage = json.get("refPage").toString();
                        System.out.println(refPage);
                        Map tmap = new HashMap();
    
                        if (bigMap.containsKey(refPage))
                            tmap = (Map) bigMap.get(refPage);
                        else {
                            tmap = new HashMap();
                        }
                        // 计数
                        String tCount = "count";
                        int pvCount = 1;
                        if (tmap.containsKey(tCount)) {
                            pvCount = (Integer) tmap.get(tCount);
                        }
                        pvCount++;
                        tmap.put(tCount, pvCount);
                        bigMap.put(refPage, tmap);
                    }
                    //返回此文件中的当前偏移量。 
                    chars = rf.getFilePointer();
                    size=fileSize;
                } catch (IOException e) {
                    e.printStackTrace();
                }catch(JSONException j){
                    
                } finally {
                    if (rf != null) {
                        try {
                            rf.close();
                        } catch (IOException e1) {
                        }
                    }
                }
                return bigMap;
            }
          //获取文件的行数
          static int getTotalLines(File file) throws IOException {
                FileReader in = new FileReader(file);
                LineNumberReader reader = new LineNumberReader(in);
                String s = reader.readLine();
                int lines = 0;
                while (s != null) {
                    lines++;
                    s = reader.readLine();
                }
                reader.close();
                in.close();
                return lines;
            }
    View Code

        

     
  • 相关阅读:
    做SQL解析时遇到的问题
    SqlAnalyzer1.00源码
    用于测试SqlAnalyzer1.00的十七个测试用例
    XmlAnalyzer1.00 源码
    用于测试XmlAnalyzer 1.00版的八个测试用例
    JsonAnalyzer2 1.01版
    用于测试 JsonAnalyzer2 1.01版的测试用例
    按照BNF语法重新写就的JsonAnalyzer2
    递归向下解析算术表达式(四)
    允许Sublime编辑器在Ubuntu上输入中文
  • 原文地址:https://www.cnblogs.com/tomcatx/p/4245773.html
Copyright © 2020-2023  润新知