日期:2014-05-19  浏览次数:20676 次

用IO流解析apache日志
路径应该怎么写,才能读取出来保存到表里

------解决方案--------------------
Java code


public class ParseFile {


    /**
     * @param path        文件路径
     * @param start        指针开始位置
     * @param len        要读取的行数
     * @throws Exception
     */
    public static void readFile(String path , long start , long len) throws Exception{
        File file = new File(path);
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        raf.seek(start);
        String line = null;
        long totalLen = 0 ;
        while(totalLen < len && (line = raf.readLine()) != null){
            System.out.println(line);
            totalLen++;
        }
        long endPoint = raf.getFilePointer();    //得到读取完指定内容后指针位置
        //这里可以将endPoint保存到数据库或文件  ,方便下次取出endPoint 然后接着读
    }

    public static void main(String[] args) throws Exception {
        readFile("C:\\Documents and Settings\\zhoufeng\\桌面\\MappingJacksonHttpMessageConverterv2.java", 60 , 2    );
    }

}