Java读取文件新增内容

文章目录
  1. 通过Java的RandomAccessFile类实现读取文件新增内容
    1. Java文件生成新增内容类(LogWriter)
    2. Java读取文件新增内容类(LogReader)

通过Java的RandomAccessFile类实现读取文件新增内容

Java文件生成新增内容类(LogWriter)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.child.fileread;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LogWriter {
private SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 将信息记录到日志文件
*
* @param logFile 日志文件
* @param mesInfo 信息
* @throws IOException
*/
public void logMsg(File logFile, String mesInfo) throws IOException {
if (logFile == null) {
throw new IllegalStateException("logFile can not be null!");
}
Writer txtWriter = new FileWriter(logFile, true);
txtWriter.write(dateFormat.format(new Date()) + "\t" + mesInfo + "\n");
txtWriter.flush();
}
public static void main(String[] args) throws Exception {
final LogWriter logSvr = new LogWriter();
final File tmpLogFile = new File("d:/upload/text.txt");
if (!tmpLogFile.exists()) {
tmpLogFile.createNewFile();
}
Random random = new Random();
//启动一个线程每1秒钟向日志文件写一次数据
ScheduledExecutorService exec =
Executors.newScheduledThreadPool(1);
exec.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
logSvr.logMsg(tmpLogFile, String.valueOf(random.nextInt()) + "你好");
} catch (IOException e) {
System.out.println("logFile can not be null");
throw new RuntimeException(e);
}
}
}, 0, 1, TimeUnit.SECONDS);
}
}

Java读取文件新增内容类(LogReader)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.child.fileread;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LogReader {
private static long lastTimeFileSize = 0; //上次文件大小
/**
* 实时输出日志信息
* @param logFile 日志文件
* @throws IOException
*/
public static void read(File logFile) throws IOException {
// 指定文件可读可写
final RandomAccessFile randomFile = new RandomAccessFile(logFile,"rw");
// 启动一个线程每1秒钟读取新增的日志信息
ScheduledExecutorService exec =
Executors.newScheduledThreadPool(1);
exec.scheduleWithFixedDelay(new Runnable(){
public void run() {
try {
//获得变化部分的
randomFile.seek(lastTimeFileSize);
String tmp = "";
while( (tmp = randomFile.readLine())!= null) {
System.out.println(new String(tmp.getBytes("ISO8859-1")));
}
lastTimeFileSize = randomFile.length();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}, 0, 1, TimeUnit.SECONDS);
}
public static void main(String[] args) throws Exception {
final File tmpLogFile = new File("d:/test.txt");
LogReader.read(tmpLogFile);
}
}