Java 如何读取文本文件的下一行,然后暂停,以便稍后读取下一行?
我编写了一个程序,根据两个常量文件,将随机数生成两个文本文件,将随机字母生成第三个文本文件。现在我需要逐行读取每个文本文件,并将它们放在一起。该计划是,发现的建议并没有真正帮助我的情况。当我尝试这种方法时,它只读取所有行,直到它完成,而不允许我选择暂停它,转到其他文件,等等 理想情况下,我想找到一些方法来阅读下一行,然后再阅读下一行。比如说某种变量来保持我在阅读中的地位Java 如何读取文本文件的下一行,然后暂停,以便稍后读取下一行?,java,bufferedreader,filereader,printwriter,bufferedwriter,Java,Bufferedreader,Filereader,Printwriter,Bufferedwriter,我编写了一个程序,根据两个常量文件,将随机数生成两个文本文件,将随机字母生成第三个文本文件。现在我需要逐行读取每个文本文件,并将它们放在一起。该计划是,发现的建议并没有真正帮助我的情况。当我尝试这种方法时,它只读取所有行,直到它完成,而不允许我选择暂停它,转到其他文件,等等 理想情况下,我想找到一些方法来阅读下一行,然后再阅读下一行。比如说某种变量来保持我在阅读中的地位 public static void mergeProductCodesToFile(String prefixFile,
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()
公共静态无效writerDomainCodeStoFile(字符串代码文件,
char fromChar,char toChar,
int numberOfCharactersPerCode,
int numberOfCodesToGenerate)引发IOException
{
对于(int i=1;i尊重您的代码,它将是这样的:
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*/}
}
可能会引发一些异常
我希望您不要用一种方法来实现它。
您也可以使用迭代器,或者一个非常简单的reader类(方法)
我不会使用列表来加载数据,至少我保证文件的大小很小,并且我可以节省内存使用。我们讨论的方法是存储数据并将其交错。正如Sergio在回答中所说,确保在文件大小和数据结构将占用多少内存方面,内存不是问题我用
//the main method we're working on
public static void mergeProductCodesToFile(String prefixFile,
String inlineFile,
String suffixFile,
String productFile) throws IOException
{
try {
List<String> prefix = read(prefixFile);
List<String> inline = read(inlineFile);
List<String> suffix = read(productFile);
String fileText = interleave(prefix, inline, suffix);
//write the single string to file however you want
} catch (...) {...}//do your error handling...
}
//helper methods and some static variables
private static Scanner reader;//I just prefer scanner. Use whatever you want.
private static StringBuilder sb;
private static List<String> read(String filename) throws IOException
{
List<String> list = new ArrayList<String>;
try (reader = new Scanner(new File(filename)))
{
while(reader.hasNext())
{ list.add(reader.nextLine()); }
} catch (...) {...}//catch errors...
}
//I'm going to build the whole file in one string, but you could also have this method return one line at a time (something like an iterator) and output it to the file to avoid creating the massive string
private static String interleave(List<String> one, List<String> two, List<String> three)
{
sb = new StringBuilder();
for (int i = 0; i < one.size(); i++)//notice no checking on size equality of words or the lists. you might want this
{
sb.append(one.get(i)).append(two.get(i)).append(three.get(i)).append("\n");
}
return sb.toString()
}
//我们正在研究的主要方法
公共静态无效mergeProductCodesToFile(字符串前缀文件,
字符串inlineFile,
字符串后缀文件,
字符串productFile)引发IOException
{
试一试{
列表前缀=读取(前缀文件);
列表内联=读取(内联文件);
列表后缀=读取(productFile);
字符串fileText=交错(前缀、内联、后缀);
//按照您的意愿将单个字符串写入文件
}catch(…){…}//执行错误处理。。。
}
//helper方法和一些静态变量
专用静态扫描仪阅读器;//我更喜欢扫描仪。可以随意使用。
私人静电发生器;
私有静态列表读取(字符串文件名)引发IOException
{
列表=新的ArrayList;
try(reader=newscanner(新文件(文件名)))
{
while(reader.hasNext())
{list.add(reader.nextLine());}
}catch(…){…}//捕获错误。。。
}
//我将在一个字符串中构建整个文件,但您也可以让此方法每次返回一行(类似于迭代器),并将其输出到文件中,以避免创建大量字符串
私有静态字符串交织(列表一、列表二、列表三)
{
sb=新的StringBuilder();
对于(int i=0;i
显然,在内存和性能方面仍有一些需要改进的地方;此外,还有一些方法可以使它稍微扩展到其他情况,但这是一个很好的起点。有了c#,我可以更容易地使用迭代器使interleave一次只给你一行,这可能会节省内存dea!大概你不能将每一行存储在某种Sting[]
或列出
,然后通过访问它们来交错它们?不同的方法,相同的最终结果。即,三个不同文件的三个数据结构。这种方法如何:我曾想过使用这样的方法,但唯一的问题是它位于顶行上方,而不是顶行右侧。因为数字每行的s由一个常数决定,我可以在该常数上加一个,但是我用什么来选择给定行上的位置呢?我对你关于行上的数字和选择行上的位置的讨论有点困惑。你为什么不发布一个你的3个文件的示例,以及你希望它们放在一起时的样子呢?这会有帮助的每个人都有一个解决方案。好的,我刚刚编辑了这个问题。例如,ABC123DEF将是一个文件的最终目标,一个文件称为ABC,另一个文件称为123,另一个文件称为DEF。前三个文件的第一行将对应于第四个文件的第一行。问题是,尽管我需要能够更改以后使用的数字数量以及有多少行。所以将来它可能会像ABCD1234EFGH,作为一个例子。虽然它是完全随机的。嗯。我喜欢你的方法的简单性,而且它确实有效,但是当我使用我的正常写作方法时,我似乎无法将它打印到下一行。它都占用了一行。你有什么想法吗是什么原因造成的?我正在使用:out.println(line);您需要添加一个换行尝试此line.append(前缀).append(内联).append(后缀).append(“\r\n”)
你的方法肯定很有趣,不过如果我使用数组,我会认为最好先用生成数字的方法创建数组。我会写一些东西,然后很快发布这种方法。这将是一个有趣的尝试。避免额外的文件