【问题标题】:Why won't my program write information to the file?为什么我的程序不会将信息写入文件?
【发布时间】:2012-04-07 17:10:47
【问题描述】:

我必须创建一个程序,从已经按字母顺序排列的两个文件中读取名称和 ID 号,然后将它们完全按字母顺序组合到第三个文件中。文件的组织方式如下...

姓,名 ID(例如 rbb091020) 姓,名 身份证

由于某种原因,我的文件读取了这两个文件并创建了第三个文件,但它没有向第三个文件写入任何内容。

这是我的代码...

import java.io.*;
import java.util.Scanner;

public class Combine_Files
{
public static void main(String[]args) throws IOException
{
    String filename1 = "";
    String filename2 = "";
    String combinedFileName = "";
    String response = "";
    String nextName1 = "";
    String nextName2 = "";
    String nextIDNumber1 = "";
    String nextIDNumber2 = "";
    boolean okToOverwrite = false;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("What is the name of the first file?");
    filename1 = keyboard.nextLine();

    File file1 = new File(filename1);

    if (!file1.exists())
    {
        System.out.println(file1 + " does not exist.\nTerminating Program.");
        System.exit(0);
    }

    System.out.println("What is the name of the second file?");
    filename2 = keyboard.nextLine();

    File file2 = new File (filename2);
    if (!file2.exists())
    {
        System.out.println(file2 + " does not exist.\nTerminating Program.");
        System.exit(0);
    }   

    System.out.println("What is the name of the file that you wish to create?");
    combinedFileName = keyboard.nextLine();
    File combinedFile = new File (combinedFileName);

    while (combinedFile.exists() && !okToOverwrite)
    {
        System.out.println("\nError, a file named " +
            combinedFileName + " already exists." +
            "\nWould you like to overwrite this file?" +  
            "\nEnter Y for Yes or N for No.");
        response = keyboard.nextLine();

        // Add input validation on response

        if (response.equalsIgnoreCase("Y"))
        {
            okToOverwrite = true;
        }
        else 
        {
            System.out.println("Enter a new filename.");
            combinedFileName = keyboard.nextLine();
            combinedFile = new File(combinedFileName);
        }
    }

    if (file1.exists() && file2.exists())
    {

        Scanner list1 = new Scanner(file1);
        Scanner list2 = new Scanner(file2);
        PrintWriter outputFile = new PrintWriter(combinedFile);

            while (list1.hasNext() && list2.hasNext())
            {
                nextName1 = list1.nextLine();
                nextName2 = list2.nextLine();

                if(nextName1.compareToIgnoreCase(nextName2) <0)
                {
                    outputFile.print(nextName1);
                    nextName1 = list1.nextLine();
                    outputFile.print(nextName1);
                }
                else if(nextName1.compareToIgnoreCase(nextName2) >0)
                {
                    outputFile.println(nextName2);
                    nextName2 = list2.nextLine();
                    outputFile.println(nextName2);
                }
                else
                {
                    outputFile.println(nextName1);
                    nextName1 = list1.nextLine();
                    outputFile.println(nextName1);
                    outputFile.println(nextName2);
                    nextName2 = list2.nextLine();
                    outputFile.println(nextName2);
                }       
            }

            while (list1.hasNext() && !list2.hasNext())
            {
                outputFile.println(nextName1);
            }

            while (list2.hasNext() && !list1.hasNext())
            {
                outputFile.println(nextName2);
            }       
    }
}   
}           

【问题讨论】:

    标签: java file-io alphabetical


    【解决方案1】:

    当您使用像PrintWriter 这样的编写器类时,您需要确保每次要将某些内容打印到STDOUT 或文件时调用flush() 方法。所以基本上每当你调用它写入内部缓冲区的任何PrintWriter 打印方法时,对flush() 的调用都会将缓冲区发送到适当的输出流。

            while (list1.hasNext() && list2.hasNext())
            {
                nextName1 = list1.nextLine();
                nextName2 = list2.nextLine();
    
                if(nextName1.compareToIgnoreCase(nextName2) <0)
                {
                    outputFile.print(nextName1);
                    nextName1 = list1.nextLine();
                    outputFile.print(nextName1);
                }
                else if(nextName1.compareToIgnoreCase(nextName2) >0)
                {
                    outputFile.println(nextName2);
                    nextName2 = list2.nextLine();
                    outputFile.println(nextName2);
                }
                else
                {
                    outputFile.println(nextName1);
                    nextName1 = list1.nextLine();
                    outputFile.println(nextName1);
                    outputFile.println(nextName2);
                    nextName2 = list2.nextLine();
                    outputFile.println(nextName2);
                }
                outputFile.flush(); // <--- flush added here       
            }
    
            while (list1.hasNext() && !list2.hasNext())
            {
                outputFile.println(nextName1);
                outputFile.flush(); // <--- flush added here 
            }
    
            while (list2.hasNext() && !list1.hasNext())
            {
                outputFile.println(nextName2);
                outputFile.flush(); // <--- flush added here 
            }       
    

    这些对flush() 的调用应该写入文件。

    【讨论】:

    • 或者在调用构造函数PrintWriter(OutputStream out, boolean autoFlush)时简单地将其设为autoFlush
    • 刷新问题是我的第一个嫌疑人,但即便如此,当句柄关闭并且 VM 退出时,应该刷新文件。这个程序还有其他问题。
    • @Eng.Fouad 根据文档,自动刷新选项仅适用于 printlnprintfformat。海报使用print()println混合,所以我选择手动flush()调用
    • @HunterMcMillen 这很奇怪!但是,close()最后的流会将数据缓冲到接收器中。
    • 我们还没有学习过flush(),并且我们不应该使用我们所学之外的东西。但是我们已经了解到,事情会卡在键盘缓冲区中。会使用keyboard.nextLine();工作冲洗吗?难道就没有别的办法冲了吗?
    【解决方案2】:

    这个程序有逻辑错误。当我使用长度不均匀的文件进行测试时,它会在最后两个 while 循环之一中陷入无限循环,并且确实会写入输出文件,直到程序被强制终止。如果文件长度相同,则扫描仪无法读取 if/else 子句之一中的一行。

    【讨论】:

    • 另外,你是如何让它写入第三个文件的?由于某种原因,它只将一个空白文件写入我的第三个文件。
    • 我不知道为什么它不会从我的脑海中写入输出文件,但行为可能会因输入而异。我的两个文件分别只包含“a”“b”“c”和“d”“e”“f”,当然每个文件都在一行。您需要通过打印输出 (System.err.println("Writing xxxx to file yyy")) 或通过步进调试器来调试此程序,以便您了解程序在做什么。
    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多