gpt4 book ai didi

java - 程序在写入 .txt 文件时出现延迟?

转载 作者:行者123 更新时间:2023-12-05 07:40:05 24 4
gpt4 key购买 nike

所以,我在 stackoverflow 上搜索了一下,但我似乎找不到这个问题的答案。

我目前的 CS 课家庭作业涉及从一个包含 5000 个随机数的文件中读取并对数据执行各种操作,例如将其放入一个数组中,查看一个数字出现了多少次,以及找出最长的递增序列是什么.我已经很好地完成了所有这些工作。

除此之外,我(为我自己)添加了一个方法,该方法允许我覆盖文件并创建 5000 个新的随机数,以确保我的代码适用于多个不同的测试用例。

该方法在大多数情况下都有效,但是在我调用它之后,它似乎直到程序的其余部分完成后才“激活”。如果我运行它并告诉它更改数字,我必须再次运行它才能在程序中实际看到更改后的值。有办法解决这个问题吗?

当前输出显示更改数据之间的延迟:

此处不尝试更改数据 - 控制案例。

elkshadow5$ ./CompileAndRun.sh

Create a new set of numbers? Y for yes. n
What number are you looking for? 66
66 was found 1 times.
The longest sequence is [606, 3170, 4469, 4801, 5400, 8014]
It is 6 numbers long.

这里的数字应该会改变,但实际上并没有。

elkshadow5$ ./CompileAndRun.sh

Create a new set of numbers? Y for yes. y
What number are you looking for? 66
66 was found 1 times.
The longest sequence is [606, 3170, 4469, 4801, 5400, 8014]
It is 6 numbers long.

现在数据显示已更改,数据应该已更改后运行。

elkshadow5$ ./CompileAndRun.sh

Create a new set of numbers? Y for yes. n
What number are you looking for? 1
1 was found 3 times.
The longest sequence is [1155, 1501, 4121, 5383, 6000]
It is 5 numbers long.

我的代码:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class jeftsdHW2 {
static Scanner input = new Scanner(System.in);

public static void main(String args[]) throws Exception {
jeftsdHW2 random = new jeftsdHW2();
int[] data;
data = new int[5000];
random.readDataFromFile(data);
random.overwriteRandNums();
}
public int countingOccurrences(int find, int[] array) {
int count = 0;
for (int i : array) {
if (i == find) {
count++;
}
}
return count;
}
public int[] longestSequence(int[] array) {
int[] sequence;
return sequence;
}
public void overwriteRandNums() throws Exception {
System.out.print("Create a new set of numbers? Y for yes.\t");
String answer = input.next();
char yesOrNo = answer.charAt(0);
if (yesOrNo == 'Y' || yesOrNo == 'y') {
writeDataToFile();
}
}
public void readDataFromFile(int[] data) throws Exception {
try {
java.io.File infile = new java.io.File("5000RandomNumbers.txt");
Scanner readFile = new Scanner(infile);

for (int i = 0; i < data.length; i++) {
data[i] = readFile.nextInt();
}
readFile.close();
} catch (FileNotFoundException e) {
System.out.println("Please make sure the file \"5000RandomNumbers.txt\" is in the correct directory before trying to run this.");
System.out.println("Thank you.");
System.exit(1);
}
}
public void writeDataToFile() throws Exception {
int j;
StringBuilder theNumbers = new StringBuilder();
try {
PrintWriter writer = new PrintWriter("5000RandomNumbers.txt", "UTF-8");
for (int i = 0; i < 5000; i++) {
if (i > 1 && i % 10 == 0) {
theNumbers.append("\n");
}
j = (int) (9999 * Math.random());
if (j < 1000) {
theNumbers.append(j + "\t\t");
} else {
theNumbers.append(j + "\t");
}
}
writer.print(theNumbers);
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println("error");
}
}
}

最佳答案

可能文件还没有被物理写入磁盘,使用 flush 是不够的,来自 java 文档 here :

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

由于 HDD 的读写速度,建议尽可能少地依赖 HDD 访问。

也许在重新运行和使用时将随机数字符串存储到列表中是一种解决方案。您甚至可以将列表写入磁盘,但这种方式的实现不依赖于写入文件的时间。

编辑

在 OP 发布更多代码后,很明显我原来的答案与问题无关。尽管如此,它还是不错的。

OP 贴出的代码不足以看出他写完后什么时候读文件。看起来他是在阅读后写入文件,这当然被认为是一个错误。先写后读应该会生成一个可以满足您要求的程序。我想,这个:

random.readDataFromFile(data); 
random.overwriteRandNums();

会一直反射(reflect)到下一次执行。这:

random.overwriteRandNums();
random.readDataFromFile(data);

将在当前执行中使用更新后的文件。

关于java - 程序在写入 .txt 文件时出现延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46518080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com