分析日誌並按分隔符分開儲存

2021-08-23 14:46:53 字數 2635 閱讀 5569

/**

* 讀取日誌檔案位址,每行根據指定字元分割開,每行分割成乙個陣列.每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(mapmap) throws exception

if (ifnull(splitcharacter))

if (ifnull(linesonce))

if (ifnull(filefrom))

if (ifnull(fileto))

fileto = filefrom.substring(0, indexdot) + "_copy" + filefrom.substring(indexdot);

} hashmapmapanlayze = 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(mapmap)

int linesonceint = integer.parseint(linesonce);

int lineread = 0; // 現在讀到了那一行

filereader fr = null;

try

hashmapmaponeline = new hashmap();

maponeline.put("splitcharacter", splitcharacter);

string oneline = (string) operateoneline(maponeline, line); // 對讀入的一行進行操作

loginfo.add(oneline);

if ((lineread % linesonceint) == 0) }}

*/loginfo.clear();

//如果不清空,差不多60000行,占用記憶體10m

}} while (line != null);

br.close();

fr.close();

} catch (ioexception e)

} catch (filenotfoundexception e)

return loginfo;

} public static string copyfile(mapmap)

if ("".equals(filefrom))

if ("".equals(fileto))

fileinputstream fis = null;

fileoutputstream fos = null;

byte buff = new byte[1024];

int readed = -1;

try catch (filenotfoundexception e)

try catch (filenotfoundexception e)

try catch (ioexception e)

} catch (exception e) finally

if (fos != null)

} catch (ioexception e)

} return errorinfo;

} public static string operateoneline(mapmap, string line)

string splitcharacters = line.split(splitcharacter);

/* 可以列印出來檢視下.

for (int i = 0; i < splitcharacters.length; i++)

*/return splitcharacters;

} /**

* 判斷輸入的字串是否為空,如果是則返回true,否則為false;

* @param str

* @return

*/public static boolean ifnull(string str) else

AWK改變輸入輸出分隔符例項分析

awk預設從stdin接受資料,列印文字到stdout。awk的預設輸入和輸出分隔符 fs 輸入字段分隔符,預設空格。rs 輸入行分隔符,預設 n。ofs 輸出字段分隔符,預設空格。ors 輸出行分隔符,預設 n。例項 通過改變預設的內建變數使多行變成一行 file.txt 每個人的資訊條數不確定,...

AWK改變輸入輸出分隔符例項分析

awk預設從stdin接受資料,列印文字到stdout。awk的預設輸入和輸出分隔符 fs 輸入字段分隔符,預設空格。rs 輸入行分隔符,預設 n。ofs 輸出字段分隔符,預設空格。ors 輸出行分隔符,預設 n。例項 通過改變預設的內建變數使多行變成一行 file.txt 每個人的資訊條數不確定,...

python檔案路徑分隔符的詳細分析

寫了挺久的python,檔案分隔符的掌握肯定是必須的,但是我之前寫的都是不規範的檔案路徑分隔符,例如 c user temp python.txt 一直都沒有報過錯。也不知為啥,今天查閱資料才知道自己寫的都是些假的python,所以就在此記錄一下。在windows系統下的分隔符是 反斜槓 在linu...