以下は、私が学校プロジェクトで取り組んでいるコードです。 animal.txtファイルを読み込もうとするまでは大丈夫です。誰かが私が間違っていることを教えてもらえますか?コンパイルエラーを画像として添付しています。前もって感謝します。netbeansを使用してJavaで入力ファイルを読み取る際に問題が発生する
[入力エラー画像1
package finalproject;
//enabling java programs
import java.util.Scanner;
import javax.swing.JOptionPane;
インポートjava.io.FileInputStream。 import java.io.IOException;
パブリッククラス監視{
public static void choseAnimal() throws IOException{
FileInputStream file = null;
Scanner inputFile = null;
System.out.println("Here is your list of animals");
file = new FileInputStream("\\src\\finalproject\\animals.txt");
inputFile = new Scanner(file);
while(inputFile.hasNext())
{
String line = inputFile.nextLine();
System.out.println(line);
}
}
public static void choseHabit(){
System.out.println("Here is your list of habits");
}
public static void main(String[] args) throws IOException{
String mainOption = ""; //user import for choosing animal, habit or exit
String exitSwitch = "n"; // variable to allow exit of system
Scanner scnr = new Scanner(System.in); // setup to allow user imput
System.out.println("Welcome to the Zoo");
System.out.println("What would you like to monitor?");
System.out.println("An animal, habit or exit the system?");
mainOption = scnr.next();
System.out.println("you chose " + mainOption);
if (mainOption.equals("exit")){
exitSwitch = "y";
System.out.println(exitSwitch);
}
if (exitSwitch.equals("n")){
System.out.println("Great, let's get started");
}
if (mainOption.equals("animal")){
choseAnimal();
}
if (mainOption.equals("habit")) {
choseHabit();
}
else {
System.out.println("Good bye");
}
}
}
に存在していることを前提としていプログラムがビルドされてパッケージ化されると、それは存在しません。 Netbeansは自動的に 'src'ディレクトリ内のすべてを結果jarファイルにパッケージ化します – MadProgrammer
また、このタイプのリソースをファイルとして扱うことはできません。また、' Class#getResource'または 'Class#getResourceAsStream'を使用して、それを読んで – MadProgrammer