为什么我的PrintWriter类没有按预期工作?

时间:2014-08-25 07:17:12

标签: java exception io printwriter

我有这个应用程序提示用户输入文本文件,从这个文本文件中,它包含整数和文本字符串。从那里开始,它应该写入另一个文本文件 result.txt 。现在,因为我还是IO的新手,尽管文件已成功创建,但我在写入文件方面遇到了问题。在用户输入文本文件的名称后,应用程序停在该部分。那么请你们给我一些帮助吗?提前谢谢!

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

class FileReadingExercise3 {

public static void main(String [] args)
{
    Scanner userInput = new Scanner(System.in);
    Scanner fileInput = null;

    String a = null;
    int sum = 0;

    do
    {
        try
        {
            System.out.println("Please enter the name of a file or type QUIT to finish");
            a = userInput.nextLine();

            if(a.equals("QUIT"))
            {
                System.exit(0);
            }

            fileInput = new Scanner(new File(a));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error " + a + " does not exist.");
        }
    }while(fileInput == null);

    PrintWriter output = null;

    try
    {
        output = new PrintWriter(new File("result.txt"));
    }
    catch(IOException g)
    {
        System.out.println("Error");
        System.exit(0);
    }

    while(fileInput.hasNext())
    {
        if(fileInput.hasNextInt())
        {
            int num = fileInput.nextInt();
            sum += num;

            String str = Integer.toString(num);

            output.println(str);

        }
    }

    fileInput.close();
    output.close();
}
}

1 个答案:

答案 0 :(得分:1)

它被卡住了,因为你必须在调用next()后调用hasNext()方法,这样指针就会转到输入文件的下一行。

此外,您没有使用sum,请检查您是否需要此变量。

以下是有效的代码:

public static void main(String[] args) throws FileNotFoundException {
        Scanner userInput = new Scanner(System.in);
        Scanner fileInput = null;
        String a = null;
        int sum = 0;
        do {
            try {
                System.out
                        .println("Please enter the name of a file or type QUIT to finish");
                a = userInput.nextLine();
                if (a.equals("QUIT")) {
                    System.exit(0);
                }
                fileInput = new Scanner(new File(a));
            } catch (FileNotFoundException e) {
                System.out.println("Error " + a + " does not exist.");
            }
        } while (fileInput == null);

        PrintWriter output = null;
        try {
            output = new PrintWriter(new File("result.txt"));
        } catch (IOException g) {
            System.out.println("Error");
            System.exit(0);
        }
        while (fileInput.hasNext()) {
            if (fileInput.hasNextInt()) {
                int num = fileInput.nextInt();
                sum += num;
                String str = Integer.toString(num);
                output.println(str);
            } else {
                fileInput.next();
            }
        }
        fileInput.close();
        output.close();
    }
}

<强>更新

根据Scanner.hasNext()方法的java doc:

  

如果此扫描器的输入中有另一个标记,则返回true。这个   方法可能会在等待输入扫描时阻塞。 扫描仪没有   超越任何输入。

因此,要转到下一个位置,您需要调用next()方法,否则扫描仪将处于相同的位置,程序会陷入无限循环。

相关问题