首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

如何用Java读txt文件,根据其中的条件,输出一个csv文件?(具体情况见内容)

2013-09-08 
【求助】怎么用Java读txt文件,根据其中的条件,输出一个csv文件?(具体情况见内容)0用Java,读txt文件(根据Ecli

【求助】怎么用Java读txt文件,根据其中的条件,输出一个csv文件?(具体情况见内容)
0
用Java,
读txt文件(根据Eclipse软件的下方的console改成的log.txt)
——————————————————————————————————————
例:
[2013-08-26 10:45:45,042] ERROR does not exist.
[2013-08-26 14:43:09,145] ERROR Certification failed.
[2013-08-26 14:43:09,145] ERROR Certification failed.
[2013-08-26 14:43:10,973] ERROR Certification failed.
[2013-08-26 14:43:33,285] ERROR Certification failed.
[2013-08-28 09:23:12,920] INFO Login succeeded.User ID=[CSDN123]
[2013-09-02 10:13:32,793] INFO Login succeeded.User ID=[CSDN123]
[2013-09-02 10:36:05,050] INFO Login succeeded.User ID=[CSDN123]
[2013-09-02 10:48:26,407] INFO Login succeeded.User ID=[CSDN123]
——————————————————————————————————————
从左至右分别是:
[时间][级别(有Error、Info、Warning等)][错误信息]

然后,根据时间(如:2013-08-26至2013-09-01),
将其中这些行的信息写成csv文件
(格式是:时间,级别(有ERROR、INFO、WARNING等),错误信息)。

或者,根据级别(如:级别是ERROR),
将这些行的信息写成csv文件,格式同上。

小弟初来乍到,求大神帮忙,实现此功能,万分感谢!

java eclipse csv txt 读写文件
[解决办法]
往CSV文件中写就是了,列用逗号分开,行用\r\n分开
[解决办法]
不明白你的意思。
如果单纯只是改格式的话,你直接把txt的后缀改成csv就行了
或者你如果想按级别输出的话,那就定义一个几个Map<"error","logmsg">这样的,最后挨个输出应该就可以了吧。

[解决办法]


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileTest {

/**
 * @param args
 */
public static void main(String[] args) throws Exception {



String txtFile = "D:/temp/log.txt";
String csvFile = "D:/temp/log.csv";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

txtToCsv(txtFile, csvFile, format.parse("2013-09-01"), null, LogLevel.INFO);
}
    
/**
 * txt file to csv file
 * @param oldFile
 * @param newFile
 * @param startTime
 * @param endTime
 * @param level
 */
public static void txtToCsv(
String oldFile, String newFile,
Date startTime, Date endTime, LogLevel level) {

String line = null;
PrintWriter pw = null;
BufferedReader buf = null;
List<Line> lines = new ArrayList<Line>();
try {
// 1. read file
buf = new BufferedReader(
new InputStreamReader(new FileInputStream(oldFile), "gbk"));
while ((line = buf.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
Line bean = Line.toBean(line);
if (bean == null 

[解决办法]
 (startTime != null && bean.getTime().before(startTime))

[解决办法]
 (endTime != null && endTime.before(bean.getTime()))

[解决办法]
 (level != null && bean.getLevel().compareTo(level) < 0)) {
continue;
}
lines.add(bean);
}

// 2. write file
pw = new PrintWriter(newFile);
for (Line bean : lines) {
pw.println(bean.toCsv());
}
pw.flush();
pw.close();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try { if (buf != null) { buf.close(); } } catch (IOException e) {}
}
}
}
enum LogLevel {
TRACE, DEBUG, INFO, WARN, ERROR
}
class Line {
private Date time;
private LogLevel level;
private String msg;

public static Line toBean(String data) {


Line bean = null;

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
String reg = "\\[([\\d-\\s:,]+)\\]\\s+(\\w+)\\s(.+)$";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(data);
if (m.matches()) {
try {
Date time = format.parse(m.group(1));
LogLevel level = LogLevel.valueOf(m.group(2));
String msg = m.group(3);
bean = new Line(time, level, msg);
}
catch (Exception e) {
// Ignore.
}
}

return bean;
}

public Line(Date time, LogLevel level, String msg) {
this.time = time;
this.level = level;
this.msg = msg;
}
    
public String toCsv() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return format.format(time) + ", " + level.name() + ", " + msg;
}

public Date getTime() {
return time;
}
    
public LogLevel getLevel() {
return level;
}
}

热点排行