当前位置: 代码迷 >> java >> 如何从文本文件的下一行读取内容并暂停,以便稍后再从该行读取内容?
  详细解决方案

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

热度:82   发布时间:2023-07-31 13:48:23.0

我编写了一个程序,根据两个常数文件,该程序将随机数生成两个文本文件,将随机字母生成第三个字符。 现在,我需要逐行阅读每个文本文件,并将它们放在一起。 该程序是,在找到的建议并不能真正解决我的问题。 当我尝试这种方法时,它只会读取所有行,直到完成为止,而不会允许我选择暂停它,转到另一个文件等。

理想情况下,我想找到某种方式来仅读取下一行,然后在此之后转到下一行。 就像某种变量可以使我在阅读中占有一席之地。

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来加载数据,至少我保证文件的大小较小,并且可以节省内存使用量。

我们讨论的方法是存储数据并进行交织。 就像塞尔吉奥在回答中所说的那样,确保就文件大小和数据结构将使用多少内存而言,内存不是问题。

//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()
}

显然,在内存和性能方面仍有一些需求。 另外,还有一些方法可以使它稍微扩展到其他情况,但这是一个很好的起点。 使用c#,我可以更轻松地利用迭代器使交错一次为您提供一行,从而可能节省内存。 只是一个不同的想法!