gpt4 book ai didi

java - 如何从文本文件的下一行读取并暂停,以便我稍后再读取该行之后的内容?

转载 作者:行者123 更新时间:2023-11-30 03:20:14 26 4
gpt4 key购买 nike

我编写了一个程序,根据两个常量文件将随机数生成到两个文本文件中,并将随机字母生成到第三个文本文件中。现在我需要逐行读取每个文本文件,并将它们放在一起。程序是建议找到的here对我的情况并没有真正的帮助。当我尝试这种方法时,它只会读取所有行,直到完成,而不允许我选择暂停它、转到不同的文件等。

理想情况下,我想找到某种方法来读取下一行,然后再转到下一行。就像某种变量来保持我在阅读中的位置或其他什么。

public static void mergeProductCodesToFile(String prefixFile,
String inlineFile,
String suffixFile,
String productFile) throws IOException
{
try (BufferedReader br = new BufferedReader(new FileReader(prefixFile)))
{
String line;
while ((line = br.readLine()) != null)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(productFile, true))))
{
out.print(line); //This will print the next digit to the right
}
catch (FileNotFoundException e)
{
System.err.println("File error: " + e.getMessage());
}
}
}
}

编辑:根据以下内容创建的数字。基本上,常量告诉它在每行中创建多少个数字以及要创建多少行。现在我需要将它们组合在一起,而不从任何一个文本文件中删除任何内容。

public static void writeRandomCodesToFile(String codeFile, 
char fromChar, char toChar,
int numberOfCharactersPerCode,
int numberOfCodesToGenerate) throws IOException
{

for (int i = 1; i <= PRODUCT_COUNT; i++)
{
int I = 0;


if (codeFile == "inline.txt")
{

for (I = 1; I <= CHARACTERS_PER_CODE; I++)
{
int digit = (int)(Math.random() * 10);


try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(codeFile, true))))
{
out.print(digit); //This will print the next digit to the right
}
catch (FileNotFoundException e)
{
System.err.println("File error: " + e.getMessage());
System.exit(1);
}

}
}

if ((codeFile == "prefix.txt") || (codeFile == "suffix.txt"))
{

for (I = 1; I <= CHARACTERS_PER_CODE; I++)
{
Random r = new Random();
char digit = (char)(r.nextInt(26) + 'a');
digit = Character.toUpperCase(digit);

try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(codeFile, true))))
{
out.print(digit);
}
catch (FileNotFoundException e)
{
System.err.println("File error: " + e.getMessage());
System.exit(1);
}

}
}
//This will take the text file to the next line
if (I >= CHARACTERS_PER_CODE)
{
{
Random r = new Random();
char digit = (char)(r.nextInt(26) + 'a');

try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(codeFile, true))))
{
out.println(""); //This will return a new line for the next loop
}
catch (FileNotFoundException e)
{
System.err.println("File error: " + e.getMessage());
System.exit(1);
}
}
}

}
System.out.println(codeFile + " was successfully created.");

}// end writeRandomCodesToFile()

最佳答案

尊重你的代码,它会是这样的:

public static void mergeProductCodesToFile(String prefixFile, String inlineFile, String suffixFile, String productFile) throws IOException {
try (BufferedReader prefixReader = new BufferedReader(new FileReader(prefixFile));
BufferedReader inlineReader = new BufferedReader(new FileReader(inlineFile));
BufferedReader suffixReader = new BufferedReader(new FileReader(suffixFile))) {

StringBuilder line = new StringBuilder();
String prefix, inline, suffix;
while ((prefix = prefixReader.readLine()) != null) {
//assuming that nothing fails and the files are equals in # of lines.
inline = inlineReader.readLine();
suffix = suffixReader.readLine();
line.append(prefix).append(inline).append(suffix).append("\r\n");
// write it
...

}
} finally {/*close writers*/}
}

可能会抛出一些异常。

我希望您不要用一种方法来实现它。您也可以使用迭代器,或者一个非常简单的阅读器类(方法)。

我不会使用 List 来加载数据,至少我保证文件大小较小并且可以节省内存使用量。

关于java - 如何从文本文件的下一行读取并暂停,以便我稍后再读取该行之后的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487537/

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