CSV文件我有一個CSV日誌文件,它包含很多這樣的行:讀取和寫入使用Java
2016-06-21 12:00:00,000 : helloworld: header1=2;header2=6;header=0
我想他們寫入到一個新的CSV文件。
public void readLogFile() throws Exception
{
String currentLine = "";
String nextLine = "";
BufferedReader reader = new BufferedReader(new FileReader(file(false)));
while ((currentLine = reader.readLine()) != null)
{
if (currentLine.contains("2016") == true)
{
nextLine = reader.readLine();
if (nextLine.contains("helloworld") == true)
{
currentLine = currentLine.substring(0, 23);
nextLine = nextLine.substring(22, nextLine.length());
String nextBlock = replaceAll(nextLine);
System.out.println(currentLine + " : helloworld: " + nextBlock);
String[] data = nextBlock.split(";");
for (int i = 0, max = data.length; i < max; i++)
{
String[] d = data[i].split("=");
map.put(d[0], d[1]);
}
}
}
}
reader.close();
}
這是我的方法寫的內容:
public void writeContentToCsv() throws Exception
{
FileWriter writer = new FileWriter(".../file_new.csv");
for (Map.Entry<String, String> entry : map.entrySet())
{
writer.append(entry.getKey()).append(";").append(entry.getValue()).append(System.getProperty("line.separator"));
}
writer.close();
}
這是我想要的輸出:
header1; header2; header3
2;6;0
1;5;1
5;8;8
...
目前,CSV文件看起來像這樣(只顯示一個數據集):
header1;4
header2;0
header3;0
任何人都可以幫我修復代碼嗎?
請使用調試器,看看我t出錯了 – Sanjeev