Exception in thread “main” java.io.FileNotFoundException: (句柄无效)

huangapple go评论116阅读模式
英文:

Exception in thread "main" java.io.FileNotFoundException: (The handle is invalid)

问题

我试图遍历一个包含多个选项的菜单。选项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;
            }
        }
    }
}

我就是无法理解为什么会出现这个错误。我已经在那里加了 throws FileNotFoundException。根据我所了解的关于 Java 和 Netbeans 的一切,当我在循环中输入一个选择时,我不应该有问题。

英文:

I'm trying loop through a menu with various options. Option 1 involves reading from a .json file.

But I keep getting this error:

Exception in thread &quot;main&quot; 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.&lt;init&gt;(FileInputStream.java:157)
at java.base/java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:112)
at java.base/java.io.FileReader.&lt;init&gt;(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) -&gt; [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

Here is the main class of my program. I am using Netbeans. I can update with the other classes if you need me to.

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 = &quot;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&quot;;
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(&quot;Enter Shift JSON Input Filename\n&quot;);
//Problem starts after this point apparently.
//Accept user input
ifileName = keyboard.nextLine();
//&quot;Open file&quot;
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(&quot;Enter Shift JSON Output Filename\n&quot;);
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;
}
}
}
}

I just can't figure out why the error shows up. I have the "throws FileNotFoundException" in there.
Everything I know about Java and Netbeans tells me that I shouldn't have a problem as soon as I enter in a choice for the loop.

答案1

得分: 0

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

Scanner keyboard = new Scanner(System.in);

while (choice != 4) {
    //首次提示菜单
    System.out.println(menu);
    choice = keyboard.nextInt();

    //除非加上这个,程序不起作用。
    ofileName = keyboard.nextLine();
}
我不知道为什么确切地说但它起作用
英文:

I eventually found that this configuration of the while loop makes the program work fine:

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();

I don't know why, exactly, but it does.

huangapple
  • 本文由 发表于 2020年10月6日 04:29:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64215768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定