简体   繁体   English

PrintWriter不写入指定的文件

[英]PrintWriter does not write to specified file

I have a program that has to read in a file calculate multiple things such as how many vowels in the file etc. For testing purposes I have just been printing the results to the console however I need it to be printed to a separate file so I am using Print Writer. 我有一个程序必须读取文件,才能计算出多种内容,例如文件中有多少个元音等。出于测试目的,我刚刚将结果打印到控制台,但是我需要将其打印到单独的文件中,所以我正在使用Print Writer。 I will include my whole code, so you can see exactly what it is doing. 我将包括我的整个代码,因此您可以确切地看到它在做什么。

   //Variables
    int vowels = 0, digits = 0, spaces = 0, upperCase = 0, lowerCase = 0;
    char ch;

    // Creates File Chooser and Scans Selected file.
    Scanner in = null;
    File selectedFile = null;
    JFileChooser chooser = new JFileChooser("E:/OOSD/src/textAnalyser");
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile();
        in = new Scanner(selectedFile);
    }

    //Loops through the file until it has counted everything. 
    while (in.hasNext()) {
        //Gets the next line from the input
        String line = in.nextLine();
        // loop goes on till it has no next line.
        for (int i = 0; i < line.length(); i++) {   
            ch = line.charAt(i);
    if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'
    || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') {
                vowels++;
            } else if (Character.isDigit(ch)) {
                digits++;
            } else if (Character.isWhitespace(ch)) {
                spaces++;
            } else if (Character.isUpperCase(ch)) {
                upperCase++;
            } else if (Character.isLowerCase(ch)) {
                lowerCase++;
            }
        }
    }//Ends While Loop.

    PrintWriter writer = new PrintWriter("Output.txt", "UTF-8");
    writer.println("Vowels: " + vowels);
    writer.println("Digits : " + digits);
    writer.println("Spaces : " + spaces);
    writer.println("Capital Letters : " + upperCase);
    writer.println("LoweCase : " + lowerCase);
    writer.close();

If you could tell me why it won't print to the output file specified that would be great thanks :) sorry that it is a pretty long question. 如果您能告诉我为什么它不能打印到指定的输出文件,那将非常感谢:)抱歉,这是一个相当长的问题。

Your PrintWriter has autoflush disabled. 您的PrintWriter已禁用自动刷新功能。 Either enable it in the constructor, or flush manually before the writer is closed. 可以在构造函数中启用它,也可以在关闭编写器之前手动刷新它。 And since you read lines, you should use in.hasNextLine() rather than in.hasNext() . 并且由于您读取了行,因此应使用in.hasNextLine()而不是in.hasNext()

for your program to work you need to flush your PrintWriter before closing it. 为了使程序正常工作,您需要先关闭PrintWriter,然后再关闭它。

    PrintWriter writer = new PrintWriter("C:/Users/r7h8/Output.txt", "UTF-8");
    writer.println("Vowels: " + vowels);
    writer.println("Digits : " + digits);
    writer.println("Spaces : " + spaces);
    writer.println("Capital Letters : " + upperCase);
    writer.println("LoweCase : " + lowerCase);
    writer.flush();
    writer.close();

When you flush it, the content is written to the file. 刷新时,内容将写入文件。

Best regards. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM