스레드를 통해 두 파일을 동시에 읽는 작업 예제를 제공 할 수 있습니까? 한 번에 읽을 수있는 가장 좋은 방법은 무엇입니까?두 파일을 동시에 Java에서 읽을 수 없습니다.
public static void main(String args[]) {
new Runnable(new File("D:/test1.log"),"thread1");
new Runnable(new File("D:/test2.log"),"thread2");
}
private static class Runnable implements Runnable {
private File logFilePath;
Thread runner;
// you should inject the file path of the log file to watch
public Runnable(File logFilePath,String threadName) {
this.logFilePath = logFilePath;
runner = new Thread(this, threadName);
runner.start();
}
_____READ LOGIC HERE____
} logs.I 어떤 가깝거나 플러시 사용하고 있지 않다 생성
프로그램.
public final class Slf4jSample {
static Logger logger = LoggerFactory.getLogger(Slf4jSample.class);
static int i=0;
public static void main(final String[] args) {
int delay = 0; // delay for 5 sec.
int period = 10000; // repeat every sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// Task here ...
logger.error("error"+i);
logger.warn("warn"+i);
logger.debug("debug"+i);
try{int i=0/0;
}catch(Exception e){
logger.error("Ecxeption"+i, e);
}
i++;
}
}, delay, period);
}
}
하지만이 파일은 계속해서 바뀌고 있습니다. 매번 업데이트 된 데이터를 가져와야합니다. – Rookie
@raghav, Ok, 프로그램을 작동시키는 방법을 편집했습니다. – Tudor
나는 이것을 시도하고있다. .. 당신은 – Rookie