指定行数读取log文件
2016-06-03 17:09
267 查看
1、读取log文件第n行数据:
2、读取log文件第n行到第n+m行数据:
3、执行读取log文件最后n行数据:
package ocm.axb.cheney.collection; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class ReadLog { //将文本读取到List中并返回,path - 文件路径,List<String> - 返回读取文件行的集合 public static List<String> getFileContent(String path) { List<String> strList = new ArrayList<String>(); try { File file = new File(path); InputStreamReader read = new InputStreamReader(new FileInputStream( file), "UTF-8"); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { strList.add(line); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return strList; } //获取指定行的值, path - 文件路径,row - 指定行, String - 返回指定行的数据,没有指定行时数据返回空字符串 public static String listFileByRow(String path, Integer row) { List<String> strList = getFileContent(path); int size = strList.size(); if (size >= (row - 1)) { return strList.get(row - 1); } else { return ""; } } public static void main(String[] args) { int startLine = 45; System.out.println("第" + startLine + "行:" + listFileByRow("E:\\db\\log4j_iws.log", startLine)); } }
2、读取log文件第n行到第n+m行数据:
package ocm.axb.cheney.collection; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class ReadLog { // 将文本读取到List中并返回,path - 文件路径,List<String> - 返回读取文件行的集合 public static List<String> getFileContent(String path) { List<String> strList = new ArrayList<String>(); try { File file = new File(path); InputStreamReader read = new InputStreamReader(new FileInputStream( file), "UTF-8"); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { strList.add(line); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return strList; } public static List<String> listFileByRegionRow(String path, Integer startLine, Integer endLine) { List<String> strList = getFileContent(path); // 指定区间的值存到regionList List<String> regionList = new ArrayList<String>(); int size = strList.size(); // 次方法获取的是第几行到第几行的数据 if (size >= (endLine - 1)) { for (int i = startLine; i <= endLine; i++) regionList.add(strList.get(i - 1)); } return regionList; } public static void main(String[] args) { //读取log文件第65行到70行的数据 int startLine = 65, endLine = 70; List<String> regionList = listFileByRegionRow("E:\\db\\log4j_iws.log", startLine, endLine); if (!regionList.isEmpty()) { for (String strLine : regionList) { System.out.println("第" + startLine + "行:" + strLine); startLine++; } } } }
3、执行读取log文件最后n行数据:
package ocm.axb.cheney.collection; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class ReadLog { // 将文本读取到List中并返回,path - 文件路径,List<String> - 返回读取文件行的集合 public static List<String> getFileContent(String path) { List<String> strList = new ArrayList<String>(); try { File file = new File(path); InputStreamReader read = new InputStreamReader(new FileInputStream( file), "UTF-8"); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { strList.add(line); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return strList; } public static List<String> listFileByRegionRow(String path, Integer endLine) { List<String> strList = getFileContent(path); // 指定区间的值存到regionList List<String> regionList = new ArrayList<String>(); int size = strList.size(); // 此处获取的是log最后多少行数据 if (size >= endLine) { for (int i = size - endLine - 1; i < size; i++) { regionList.add(strList.get(i)); } } return regionList; } public static void main(String[] args) { // 读取log文件最后5行数据 int lastLine = 5; List<String> regionList = listFileByRegionRow("E:\\db\\log4j_iws.log", lastLine); if (!regionList.isEmpty()) { for (String strLine : regionList) { System.out.println(strLine); } } } }
相关文章推荐
- sqlserver2008r2数据库关联孤立账号的方法
- excel 连接多个单元格
- ffmpeg windows install
- 【leetcode】237. Delete Node in a Linked List
- IBM MQ 当有数据传输队列的时候通道开启
- sqlserver2008r2数据库关联孤立账号的方法
- 软件工程课程建议
- Eclipse一直在computing additional info
- 通过VMCI实现VMware虚拟机与实体机、虚拟机与虚拟机的通信
- React native 'React.CreatClass is undefind'
- 前端不为人知的一面--前端冷知识集锦
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
- Android从按下开机键到启动发生了什么
- POJ 1716 Integer Intervals【差分约束+SPFA】
- Android重启app功能实现
- MySQL中获取当前时间与时间戳
- 墨菲定律、二八法则、马太效应、手表定理、“不值得”定律、彼得原理、零和游戏、华盛顿合作规律、酒与污水定律、水桶定律、蘑菇管理原理、钱的问题、奥卡姆剃刀等13条是左右人生的金科玉律
- memory库函数的实现
- 分享一个我的JavaScript版GridView多功能表格
- 优惠购书