Java,从一个文件复制到另一个文件,逐行间隔
Java, copy from a file to another, line by line with an interval
我有一个包含 120 行的文件,我想将它们一个一个地移动到另一个文件,间隔例如 1 秒,以便能够在 10 秒后在新文件中找到 10 行。
但是对于我的情况,我在新文件中执行了 0 行的程序直到结束,然后我找到了数据。
String sourceFileName = "D:\oldfile.txt";
String destinationFileName = "D:\newfile.txt";
if(evt.getSource() == btnProcess)
{
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader(sourceFileName));
pw = new PrintWriter(new FileWriter(destinationFileName));
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
Thread.sleep(1000);
}
br.close();
pw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
其次,要在同一时刻以不同的间隔处理 4 个文件,我需要使用线程吗?
感谢您的帮助。
当您写入文本文件时,PrintWriter
不会立即将其写入磁盘。相反,它将数据保存在内存中的缓冲区中。
您可以在需要将数据存入磁盘时手动刷新缓冲区。在 println()
调用 flush()
之后,如下所示。
while ((line = br.readLine()) != null) {
pw.println(line);
pw.flush();
Thread.sleep(1000);
}
你可以叫一个
pw.flush();
紧接着
pw.println(line);
这应该可以解决问题。
至于你的第二部分,如果你不想使用线程,你可以这样做:
public static void main(final String[] args) {
FileCopyDto[] files = new FileCopyDto[] {
new FileCopyDto("D:\oldfile.txt", "D:\newfile.txt", 5),
new FileCopyDto("D:\oldfile2.txt", "D:\newfile2.txt", 1)
};
try {
boolean dataAvailable = true;
int secondCount = 0;
while (dataAvailable) {
dataAvailable = false;
for (FileCopyDto d : files) {
d.write(secondCount);
dataAvailable = dataAvailable || d.isDataAvailable();
}
secondCount++;
Thread.sleep(1000);
}
for (FileCopyDto d : files) {
d.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
static class FileCopyDto {
String sourceFileName;
String destinationFileName;
int timeInSeconds;
BufferedReader br = null;
PrintWriter pw = null;
String nextLine;
public FileCopyDto(final String sourceFileName,
final String destinationFileName,
final int timeInSeconds) {
this.sourceFileName = sourceFileName;
this.destinationFileName = destinationFileName;
this.timeInSeconds = timeInSeconds;
}
public void open() throws IOException {
br = new BufferedReader(new FileReader(sourceFileName));
pw = new PrintWriter(new FileWriter(destinationFileName));
}
public boolean isDataAvailable() throws IOException {
if (br == null) {
open();
}
return (nextLine == null) || ((nextLine = br.readLine()) != null);
}
public void write(final int secondCount) {
if (nextLine != null && secondCount % timeInSeconds == 0) {
pw.println(nextLine);
pw.flush();
nextLine = null;
}
}
public void close() throws IOException {
br.close();
pw.close();
br = null;
}
}
我有一个包含 120 行的文件,我想将它们一个一个地移动到另一个文件,间隔例如 1 秒,以便能够在 10 秒后在新文件中找到 10 行。
但是对于我的情况,我在新文件中执行了 0 行的程序直到结束,然后我找到了数据。
String sourceFileName = "D:\oldfile.txt";
String destinationFileName = "D:\newfile.txt";
if(evt.getSource() == btnProcess)
{
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader(sourceFileName));
pw = new PrintWriter(new FileWriter(destinationFileName));
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
Thread.sleep(1000);
}
br.close();
pw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
其次,要在同一时刻以不同的间隔处理 4 个文件,我需要使用线程吗? 感谢您的帮助。
当您写入文本文件时,PrintWriter
不会立即将其写入磁盘。相反,它将数据保存在内存中的缓冲区中。
您可以在需要将数据存入磁盘时手动刷新缓冲区。在 println()
调用 flush()
之后,如下所示。
while ((line = br.readLine()) != null) {
pw.println(line);
pw.flush();
Thread.sleep(1000);
}
你可以叫一个
pw.flush();
紧接着
pw.println(line);
这应该可以解决问题。
至于你的第二部分,如果你不想使用线程,你可以这样做:
public static void main(final String[] args) {
FileCopyDto[] files = new FileCopyDto[] {
new FileCopyDto("D:\oldfile.txt", "D:\newfile.txt", 5),
new FileCopyDto("D:\oldfile2.txt", "D:\newfile2.txt", 1)
};
try {
boolean dataAvailable = true;
int secondCount = 0;
while (dataAvailable) {
dataAvailable = false;
for (FileCopyDto d : files) {
d.write(secondCount);
dataAvailable = dataAvailable || d.isDataAvailable();
}
secondCount++;
Thread.sleep(1000);
}
for (FileCopyDto d : files) {
d.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
static class FileCopyDto {
String sourceFileName;
String destinationFileName;
int timeInSeconds;
BufferedReader br = null;
PrintWriter pw = null;
String nextLine;
public FileCopyDto(final String sourceFileName,
final String destinationFileName,
final int timeInSeconds) {
this.sourceFileName = sourceFileName;
this.destinationFileName = destinationFileName;
this.timeInSeconds = timeInSeconds;
}
public void open() throws IOException {
br = new BufferedReader(new FileReader(sourceFileName));
pw = new PrintWriter(new FileWriter(destinationFileName));
}
public boolean isDataAvailable() throws IOException {
if (br == null) {
open();
}
return (nextLine == null) || ((nextLine = br.readLine()) != null);
}
public void write(final int secondCount) {
if (nextLine != null && secondCount % timeInSeconds == 0) {
pw.println(nextLine);
pw.flush();
nextLine = null;
}
}
public void close() throws IOException {
br.close();
pw.close();
br = null;
}
}