简体   繁体   中英

Java (Read and write to file)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class backup {

    public static void main(String[] args) {
        System.out.println("\nThe names in the file are:\n");
        readFromFile();
    }

    public static void readFromFile() {
        try {
            Scanner file = new Scanner(new File("Names.txt"));
            PrintWriter pass = new PrintWriter("pass.txt");
            PrintWriter fail = new PrintWriter("fail.txt");

            Scanner input = new Scanner(System.in);
            while (file.hasNext()) {
                String name = file.nextLine();
                System.out.printf("Please enter " + name + "'s mark:");
                int mark = input.nextInt();
                Scanner kybd = new Scanner(System.in);
                {
                    if (mark >= 40) {
                        // System.out.println(name + " has passed");
                        pass.println(name);
                    } else {
                        // System.out.println(name + " has failed");
                        fail.println(name);
                    }
                }
            } // end while
        } // end try
        catch (IOException ioe) {
            System.out.println("Error handling file");
        } // end catch
    }// readFromFile
}

Unable to write to the file pass and fail the Names file is just a list of names if the user scores over 40 they go into the pass however i cant get the txt file to populate the output.

When i insert my printwriter in the while it is only the last entry which gets added to either pass or fail txt file.

The PrintWriter only really writes when they are closed ( close ) or flushed ( flush or maybe when it thinks it is time ;-)).

Try to use try-with-resources whenever possible, that way you do not need to think about closing/flushing a stream or writer. try-with-resources takes care that AutoClosable -implementations are closed automatically at the end of the try -block.

Your sample rewritten with try-with-resources (only showing the initial part):

try(
  Scanner file = new Scanner(new File("Names.txt"));
  PrintWriter pass = new PrintWriter("pass.txt");
  PrintWriter fail = new PrintWriter("fail.txt")
) {

The rest can stay as is... or if you like: you may also want to lookup the Files-API . Maybe using Files.lines is something for you?

You should call the function close on your file for it to be written on the dsisk.

Otherwise you modifications are only in memory

Something like: file.close()

If you use PrintWriter , you have to close the stream at the end. Otherwise, it will be only in memory. Try this:

    try {
        Scanner file = new Scanner(new File("Names.txt"));
        PrintWriter pass = new PrintWriter("pass.txt");
        PrintWriter fail = new PrintWriter("fail.txt");

        Scanner input = new Scanner(System.in);
        while (file.hasNext()) {
            String name = file.nextLine();
            System.out.printf("Please enter " + name + "'s mark:");
            int mark = input.nextInt();
            //Scanner kybd = new Scanner(System.in);
            //{
                if (mark >= 40) {
                    // System.out.println(name + " has passed");
                    pass.println(name);
                } else {
                    // System.out.println(name + " has failed");
                    fail.println(name);
                }
            //}
        } // end while
        pass.close();
        fail.close();
    } // end try
    catch (IOException ioe) {
        System.out.println("Error handling file");
    }

You need to make sure that you add a finally block at the end and call the close method on anything that is closeable so in this case on the printwriter. In this case pass.close() and fail.close().

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