在Java中简单的读写log或者txt文件内容
2011-01-28 12:46
585 查看
废话就不多说了,直接进入主题:代码如下:
1.读取log或者txt文本信息
/**
* 读取log或者txt信息
*
* @param filePath
* @return
*/
public List<String> readLog(String filePath) {
List<String> list = new ArrayList<String>();
try {
FileInputStream is = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
if (line.equals(""))
continue;
else
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取一行数据时出错");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件读取路径错误FileNotFoundException");
}
return list;
}
2.写入log或者txt文件内容
/**
* 新建log或者txt文件并写入内容
*
* @param filePath
* @param fileName
* @param msg
*/
public void createLog(String filePath, String fileName, String msg) {
PrintWriter logPrint = null;
try {
logPrint = new PrintWriter(
new FileWriter(filePath + fileName, true), true);
} catch (IOException e) {
(new File(filePath)).mkdir();
try {
logPrint = new PrintWriter(new FileWriter(filePath + fileName,
true), true);
} catch (IOException ex) {
logPrint = new PrintWriter(System.err);
writerErrorInfo(logPrint, ex, "无法打开日志文件:" + filePath + fileName);
}
}
writerLogInfo(logPrint, msg);
}
/**
* 将文本信息写入日志文件
*
* @param msg
* 日志内容
*/
private synchronized static void writerLogInfo(PrintWriter logPrint,
String msg) {
logPrint.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date())
+ " " + msg);
}
/**
* 将文本信息与异常写入日志文件
*
* @param e
* @param msg
*/
private synchronized static void writerErrorInfo(PrintWriter logPrint,
Throwable e, String msg) {
logPrint.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date())
+ " " + msg);
e.printStackTrace(logPrint);
}
注意的是:
filePath必须是这样的:“盘号://文件夹名称//”
fileName必须是这样的:test.txt 或者test.log
1.读取log或者txt文本信息
/**
* 读取log或者txt信息
*
* @param filePath
* @return
*/
public List<String> readLog(String filePath) {
List<String> list = new ArrayList<String>();
try {
FileInputStream is = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
if (line.equals(""))
continue;
else
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取一行数据时出错");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件读取路径错误FileNotFoundException");
}
return list;
}
2.写入log或者txt文件内容
/**
* 新建log或者txt文件并写入内容
*
* @param filePath
* @param fileName
* @param msg
*/
public void createLog(String filePath, String fileName, String msg) {
PrintWriter logPrint = null;
try {
logPrint = new PrintWriter(
new FileWriter(filePath + fileName, true), true);
} catch (IOException e) {
(new File(filePath)).mkdir();
try {
logPrint = new PrintWriter(new FileWriter(filePath + fileName,
true), true);
} catch (IOException ex) {
logPrint = new PrintWriter(System.err);
writerErrorInfo(logPrint, ex, "无法打开日志文件:" + filePath + fileName);
}
}
writerLogInfo(logPrint, msg);
}
/**
* 将文本信息写入日志文件
*
* @param msg
* 日志内容
*/
private synchronized static void writerLogInfo(PrintWriter logPrint,
String msg) {
logPrint.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date())
+ " " + msg);
}
/**
* 将文本信息与异常写入日志文件
*
* @param e
* @param msg
*/
private synchronized static void writerErrorInfo(PrintWriter logPrint,
Throwable e, String msg) {
logPrint.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date())
+ " " + msg);
e.printStackTrace(logPrint);
}
注意的是:
filePath必须是这样的:“盘号://文件夹名称//”
fileName必须是这样的:test.txt 或者test.log
相关文章推荐
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- Java如何读取数据文件,如txt文件或者.dat文件 中的内容
- java对txt文本文件的复制粘贴(对文件内容进行了简单的修改)以行为一个处理单位
- Java 文件读写(txt类型读写并追加内容)
- java创建txt文件并存入内容
- Java读写txt文件
- 关于JSON数据的入门了解以及在java中简单的读写json文件
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- Java读写txt文件中文乱码问题
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- 记录java 流(实现简单的文件读写)
- java中,对文件进行追加内容到txt的三种方法
- Java读取文件txt里面的内容
- 如何用java来进行文件切割和简单的内容过滤
- java 简单的读写文件
- Java Gzip 不解压,直接读取txt文件内容
- Java IO 之文件读写简单实例
- java 如何将两个txt文件内容写入到一个txt文件中?
- Java简单文件读写
- 遍历文件夹access_log中的所有文件localhost_access_log*.txt,提取IP插入数据库中java代码