/**
* 读取日志文件地址,每行根据指定字符分割开,每行分割成一个数组.每20000行(可传入参数指定,linesOnce)放到一个list中,
* 进行一个操作,然后list清空,重复下一个20000行(可传入参数指定,linesOnce)
* 注意:现在是把传入的参数取出来在检查一下,重新付给新的map当参数传入,如果传入的map中参数齐全,就可以直接传入的map参数往下传
* splitCharacter 分割用的字符,linesOnce为一次读取的行数,fileFrom需要分析的文件的地址和文件名,需要完整的路径和扩展名
* fileTo是文件复制到什么地方,如果为空则是在fileFrom同样的位置,文件名是在文件名的扩展名前面加上"_copy",如:E:\\text.txt,
* 则默认的fileTo为E:\\text_copy.txt.
* @param map
* @throws Exception
*/
public static void readAndOperateLog(Map<String, String> map) throws Exception {
String splitCharacter = ""; // 分隔符,如果没有默认为"-";
String linesOnce = ""; // 一次读取多少行
String fileFrom = "E:\\test.txt"; // 源文件地址,不能为空
String fileTo = ""; // 源文件复制到地址.
if (map != null) {
splitCharacter = map.get("splitCharacter");
linesOnce = map.get("linesOnce");
fileFrom = map.get("fileFrom");
fileTo = map.get("fileTo");
}
if (ifNull(splitCharacter)) {
splitCharacter = "-";
}
if (ifNull(linesOnce)) {
linesOnce = "20000";
}
if (ifNull(fileFrom)) {
throw new Exception("源文件不能为空");
}
if (ifNull(fileTo)) {
int indexDot = fileFrom.lastIndexOf(".");
if (indexDot == -1) {
throw new Exception("请输入完成的文件路径和名称,包括扩展名!");
}
fileTo = fileFrom.substring(0, indexDot) + "_copy" + fileFrom.substring(indexDot);
}
HashMap<String, String> mapAnlayze = new HashMap();
mapAnlayze.put("splitCharacter", splitCharacter);
mapAnlayze.put("linesOnce", linesOnce);
mapAnlayze.put("fileFrom", fileFrom);
mapAnlayze.put("fileTo", fileTo);
List logInfo = logAnalyze(mapAnlayze); // 注意,如果 传入的map参数直接包含有这些参数,可以直接传入map即可
}
public static List logAnalyze(Map<String, String> map) {
List logInfo = new ArrayList();
String splitCharacter = ""; // 分割用的字符
String linesOnce = ""; // 一次读取多少行
String fileFrom = ""; // 源文件地址.
String fileTo = ""; // 复制到文件地址.
if (map != null) {
splitCharacter = map.get("splitCharacter");
linesOnce = map.get("linesOnce");
fileFrom = map.get("fileFrom");
fileTo = map.get("fileTo");
}
int linesOnceInt = Integer.parseInt(linesOnce);
int lineread = 0; // 现在读到了那一行
FileReader fr = null;
try {
HashMap<String, String> mapCopy = new HashMap();
mapCopy.put("fileFrom", fileFrom);
mapCopy.put("fileTo", fileTo);
String copyFileInfo = copyFile(mapCopy); // copy 一份文件.如果
fr = new FileReader(fileTo);
BufferedReader br = new BufferedReader(fr);
String line;
int listNum = 0;
try {
do {
line = br.readLine();
lineread++; // 假如linesOnceInt=2
if (line == null) {
break;
}
HashMap<String, String> mapOneLine = new HashMap<String, String>();
mapOneLine.put("splitCharacter", splitCharacter);
String[] oneLine = (String[]) operateOneLine(mapOneLine, line); // 对读入的一行进行操作
logInfo.add(oneLine);
if ((lineread % linesOnceInt) == 0) {
// 在这里进行一些操作。
// 在这里进行一些操作。
listNum++;
// 在这里可以打印出来查看结果.
/*
* if (logInfo != null) {
for (int i = 0; i < logInfo.size(); i++) {
String[] logInfoOneLine = (String[]) logInfo.get(i);
for (int j = 0; j < logInfoOneLine.length; j++) {
System.out.println(listNum + "::第" + ((listNum - 1) * linesOnceInt + i) + "行,数组中的第" + j + "个数据的值为" + logInfoOneLine[j]);
}
}
}
*/
logInfo.clear();
//如果不清空,差不多60000行,占用内存10M
}
} while (line != null);
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return logInfo;
}
public static String copyFile(Map<String, String> map) {
String errorInfo = "";
String fileFrom = "";
String fileTo = "";
String copyWhere = ""; // 如果需要复制到指定地方或者
if (map != null) {
fileFrom = map.get("fileFrom");
fileTo = map.get("fileTo");
}
if ("".equals(fileFrom)) { // 如果输入的复制源和复制地址为空,则提示返回信息 。
errorInfo = "复制源文件路径和文件名为空!";
return errorInfo;
}
if ("".equals(fileTo)) {
errorInfo = "复制到文件路径和文件名为空!";
return errorInfo;
}
FileInputStream fis = null;
FileOutputStream fos = null;
byte[] buff = new byte[1024];
int readed = -1;
try {
try {
fis = new FileInputStream(fileFrom);
} catch (FileNotFoundException e) {
e.printStackTrace();
errorInfo = "系统找不到指定文件!";
return errorInfo;
}
try {
fos = new FileOutputStream(fileTo);
} catch (FileNotFoundException e) {
e.printStackTrace();
errorInfo = "无法复制到指定地址!";
return errorInfo;
}
try {
while ((readed = fis.read(buff)) > 0)
fos.write(buff, 0, readed);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return errorInfo;
}
public static String[] operateOneLine(Map<String, String> map, String line) {
List oneline = new ArrayList();
String splitCharacter = "";
if (map != null) {
splitCharacter = map.get("splitCharacter");
}
String[] splitCharacters = line.split(splitCharacter);
/* 可以打印出来查看下.
for (int i = 0; i < splitCharacters.length; i++) {
// System.out.println(splitCharacters[i]);
}
*/
return splitCharacters;
}
/**
* 判断输入的字符串是否为空,如果是则返回true,否则为false;
* @param str
* @return
*/
public static boolean ifNull(String str) {
if ("".equals(str) || str == null) {
return true;
} else {
return false;
}
分析日志并按分隔符分开保存
最新推荐文章于 2024-01-15 17:31:38 发布
本文介绍了一种批量处理日志文件的方法,通过按指定行数分割并处理日志内容,实现高效的数据分析。文章详细解释了如何读取日志文件、按行分割、批量处理及结果展示。
3094

被折叠的 条评论
为什么被折叠?



