阅读和写入文本文件

时间:2014-01-20 13:49:19

标签: java io

我已编写此代码以读取文件,然后为文件中的每个名称请求标记。如果标记超过40,则传递及以下是失败并将每个名称写入相应的文件。但我在第27行收到错误:while(namesFile.hasNext()这是我的代码:

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

public class TestResults {
    public static void main(String[] args) {
        String errs = "";
        Scanner k = new Scanner(System.in);

        try {
            try (
                    Scanner namesFile = new Scanner(new File("Names.txt"));

                    PrintWriter passFile = new PrintWriter("Pass.txt");
                    PrintWriter failFile = new PrintWriter("Fail.txt");) {

                while (namesFile.hasNext()) {
                    try {
                        String tempLine = namesFile.nextLine();
                        System.out.println("Please Enter Mark For " + tempLine + " : ");
                        int mark = k.nextInt();
                        if (mark >= 40) {
                            passFile.println(tempLine + " " + mark + "%");
                        } else {
                            failFile.println(tempLine + " " + mark);
                        }
                    } catch (InputMismatchException ime) {
                        String valueStr = namesFile.next();
                        errs += "\n\t" + valueStr;
                    } finally {
                        namesFile.close();
                        passFile.close();
                        failFile.close();
                    }
                }
            }
        } // Checks to see if file is there.
        catch (IOException ioe) {
            System.out.println("ERROR: " + ioe.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

public class TestResults {

   public static void main(String[] args) {
    Scanner namesFile;
    PrintWriter passFile;
    PrintWriter failFile;


     String errs = "";
    Scanner k = new Scanner(System.in);

    try {
        try {
                namesFile = new Scanner(new File("D:/Names.txt"));

                passFile = new PrintWriter("D:/Pass.txt");
                failFile = new PrintWriter("D:/Fail.txt");



                try {
                    while (namesFile.hasNext()) {
                    String tempLine = namesFile.nextLine();
                    System.out.println("Please Enter Mark For " + tempLine + " : ");
                    int mark = k.nextInt();
                    if (mark >= 40) {
                        passFile.println(tempLine + " " + mark + "%");
                    } else {
                        failFile.println(tempLine + " " + mark);
                    }
                    }
                } catch (InputMismatchException ime) {
                    String valueStr = namesFile.next();
                    errs += "\n\t" + valueStr;
                } finally {
                    namesFile.close();
                    passFile.close();
                    failFile.close();
                }



        }
        catch(IOException ioe){
            System.out.println("ERROR: " + ioe.getMessage());
        }



    } // Checks to see if file is there.
    catch (Exception e) {

    }

  }
}