读取WEB-INF下的日志文件并添加到集合(windows和linux环境通用)

来源:互联网 发布:2017淘宝双12报名时间 编辑:程序博客网 时间:2024/05/16 10:00

public class getLogList{

//获取日志文件

public List<String>  readTxtFile(){
String logPath = this.getLogPath();
List<String> list=new ArrayList<String>();
         try {
             String encoding="GBK";
             File file=new File(logPath);
             if(file.isFile() && file.exists()){ //判断文件是否存在
                 InputStreamReader read = new InputStreamReader(
                 new FileInputStream(file),encoding);//考虑到编码格式
                 BufferedReader bufferedReader = new BufferedReader(read);
                 String lineTxt = null;
                 while((lineTxt = bufferedReader.readLine()) != null){
                list.add(lineTxt);
                 }
                 read.close();
         }else{
             logger.debug("找不到指定的文件");
         }
         } catch (Exception e) {
        logger.debug(e.toString());
         }
return list;
    }

//不同环境下获取日志文件路径
public  String getLogPath() {
String classPath = RuntimeLogController.class.getClassLoader().getResource("/").getPath();
String logPath="";
String rootPath = "";
String se=File.separator;
if("\\".equals(se)){//windows下
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
if("/".equals(se)){//linux下
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
logPath=rootPath+se+"WEB-INF"+se+"data"+se+"logs"+se+"epolice.log";
return logPath;

}

}

0 0
原创粉丝点击