简体   繁体   中英

Try Catch logic error with file i/o in Java

So, I've been trying to learn java from various sources, I've been learning for about 2 years now. So far everything has been going smoothly, i haven't had to post on stackoverflow for a while. Recently I've been trying to figure out how to create and read files with java. I can do both of those things in separate apps, but when i try to do both it doesn't always work.

What i want to happen: I want my program to create data.txt , then I want it to read the data and produce an error log on error.txt .

What happens: The data.txt file gets created as expected, but nothing is written to the error.txt file. I'm having trouble grasping the try/catch block and how exactly it works. Anyone got any ideas? even just some advice would be appreciated. Thanks!

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


public class dataReader {

    public static void main(String args[]) throws Exception {
        File fileName;
        fileName = new File("data.txt");
        PrintWriter outputFile;
        outputFile = new PrintWriter(fileName);

        File errorFile;
        errorFile = new File("errors.txt");
        PrintWriter outputErrorFile;
        outputErrorFile = new PrintWriter(errorFile);

        Scanner inputFile;



        int recordNumber = 0;


        String inputData;



        outputFile.println(77);
        outputFile.println("Fred");
        outputFile.println(92);
        outputFile.println("Wilma");
        outputFile.println(89.9);
        outputFile.println("Barney");
        outputFile.println(42);
        outputFile.println("BettyS");

        inputFile = new Scanner(fileName);

        while (inputFile.hasNext()) {

            recordNumber++;

            try {
                inputData = inputFile.nextLine();
                if (Integer.parseInt(inputData) < 50) {

                    outputErrorFile.println(recordNumber + ", " + inputData + ", is less than 50.");
                } else if (Integer.parseInt(inputData) > 90) {
                    outputErrorFile.println(recordNumber + ", " + inputData + ", is less than 50.");
                }
            } catch (Exception e) {
                outputErrorFile.println(recordNumber + ",  That's not an integer.");
            }

        }



        outputFile.close();
        outputErrorFile.close();


        System.out.println("Program terminated.");

    }
}

Move the outputFile.close(); line before inputFile = new Scanner(fileName); . Currently it's just cached in the memory and not written actually to the disk.

The documentation of PrintWriter says it all. The PrintWriter(Writer) constructor creates a writer which is not automatically flushed.

You have to call close or flush method to write your data to the file.

So you have to use outputFile.close(); method before starting reading.

and as a good practice you have to close all your PrintWriter instances to avoid memory leak.

just in this case please add inputFile.close(); at the end of your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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