线程“主”中的异常java.io.FileNotFoundException:句柄无效

问题描述

我正在尝试遍历具有各种选项的菜单。选项1涉及读取.json文件

但是我一直收到此错误

Exception in thread "main" java.io.FileNotFoundException:  (The handle is invalid)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)
    at com.malikrobinson.presentation.Main.main(Main.java:43)
Command execution Failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:764)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:711)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:289)
    at org.apache.maven.plugin.DefaultBuildpluginManager.executeMojo (DefaultBuildpluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  5.264 s
Finished at: 2020-10-05T16:17:35-04:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on project YouDontNeedToKNow: Command execution Failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors,re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions,please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

这是我程序的主要类。我正在使用Netbeans。如果您需要我可以与其他班级进行交流。

package com.substitue.presentation;

import com.substitue.business.Shift;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Scanner;

/**
 *
 * @author Notrelevant
 */
public class Main {
    public static void main(String args[]) throws FileNotFoundException{
        int choice;
        String ifileName;
        String ofileName;
        String menu = "Shift UI\n--------\n1 – Read shift from file as JSON\n2 – Write shift to file as JSON\n3 – Show shift info on screen\n4 - Exit\nEnter Choice\n";
        
        Shift first = new Shift();
        
        //First prompting of the menu
        Scanner keyboard = new Scanner(system.in);
        System.out.println(menu);
        choice = keyboard.nextInt();
        
        
        while (choice != 4) {
            switch(choice)
            {
                case 1:
                    //Print prompt
                    System.out.println("Enter Shift JSON Input Filename\n");

                    //Problem starts after this point apparently.

                    //Accept user input
                    ifileName = keyboard.nextLine();
                    //"Open file"
                    FileReader infile = new FileReader(ifileName);
                    
                    //Call the method that reads from the file
                    first.readJSON(infile);
                    
                    //Re-print the menu and prompt the user
                    System.out.println(menu);
                    choice = keyboard.nextInt();
                    break;
                case 2:
                    //Write shift to file as JSON
                    System.out.println("Enter Shift JSON Output Filename\n");
                    ofileName = keyboard.nextLine();
                    PrintStream outfile = new PrintStream(ofileName);
                    first.writeJSON(outfile);
                    
                    System.out.println(menu);
                    choice = keyboard.nextInt();
                    break;
                case 3:
                    System.out.println(first.toString());
                    
                    System.out.println(menu);
                    choice = keyboard.nextInt();
                    //Show shift info on screen
                    break;
            }
        }
    }
}

我只是不知道为什么会出现错误。我那里有“引发FileNotFoundException”。 我对Java和Netbeans所了解的一切都告诉我,只要为循环输入选项,我就不会有问题。

解决方法

我最终发现while循环的这种配置使程序可以正常工作:

Scanner keyboard = new Scanner(System.in);
    
    while (choice != 4) {
        //First prompting of the menu
        System.out.println(menu);
        choice = keyboard.nextInt();
        
        //Program does no work unless this is here.
        ofileName = keyboard.nextLine();

我不知道为什么,但是确实如此。