我有一個.txt文件,我希望它讀取該文件的內容以添加記錄選項功能,但是我在運行時遇到此錯誤:課程時間表添加,查看,更新和刪除記錄
該系統找不到指定的文件
package mylib1;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.*;
public class Schedule {
public static void executeTask(int option){
char addmore='n';
switch(option){
case 0:
return;
case 1:
do{
add();
addmore = getContinue("add");
if(addmore=='n')
break;
}while(true);
break;
case 2:
do{
view();
addmore = getContinue("view");
if(addmore=='n')
break;
}while(true);
break;
}
}
public static char getContinue(String methodName){
char ch='n';
try{
System.out.println("Do you want to " +methodName + " more records (y/n)?");
ch = (char) System.in.read();
}catch(IOException e){
System.out.println("IOException in input....");
}
return ch;
}
public static void add() {
char ch;
System.out.println("Add Record");
System.out.println("---------------");
System.out.println();
File file = new File("readcoursefile.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
try{
System.out.println("Do you want to save/cancel record s/c");
ch = (char) System.in.read();
}catch(IOException e){
System.out.println("IOException in input....");
}
}
public static void update(){
System.out.println("Update Record");
System.out.println("---------------");
System.out.println();
}
public static void view(){
System.out.println("View Record");
System.out.println("---------------");
System.out.println();
}
public static void delete(){
System.out.println("Delete Record");
System.out.println("---------------");
System.out.println();
//prompt user for console input (attributes)
//write/update user record from file.
}
public static void search(){
System.out.println("Search Record");
System.out.println("---------------");
System.out.println();
}
}
看起來像文件不存在,那裏系統看起來,請把文件放到適當的位置,或提供滿文件路徑。使系統可以很容易地 –