2015-12-03 58 views
1

我有一個.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(); 



     } 



    } 
+0

看起來像文件不存在,那裏系統看起來,請把文件放到適當的位置,或提供滿文件路徑。使系統可以很容易地 –

回答

1

你不指定用於該線路上的文本文件的完整路徑:

File file = new File("readcoursefile.txt"); 

因此,您的程序將搜索位於應用程序的working directory(與編譯的應用程序幾乎總是相同的文件夾)的位置列表中的文件。

如果您使用IDE,這可能是一個/調試或類似的地方。

您可以檢查此工作目錄是用類似下面一行的內容:

String curDir = System.getProperty("user.dir"); 

而且你可以按如下方式更改目錄:

System.setProperty("user.dir", "/tmp"); 


你有四個選項真的:

  1. 硬編碼的完整路徑(例如:File file = new File("c:\coursefiles\readcoursefile.txt");

  2. 添加一個配置文件變量文件位置

  3. 提示用戶指定的路徑。

  4. 繼續使用相對路徑並確保文件放置在其中。

0

你的代碼工作well.You只需要提供正確的路徑爲@ Sk93建議

import java.io.FileNotFoundException; 
import java.util.Scanner; 


import java.io.*; 

public class Schedule { 
public static void executeTask(int option){ 
System.out.println("i am in executeTask"); 
      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");//Here you need to provide proper path.You just go to your file location and copy the path and paste here 

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



     } 

public static void main(String []args) 
{ 
Schedule sh=new Schedule(); 
System.out.println("i am in main"); 
sh.executeTask(1); 
} 


    } 
+0

CID \t課程名稱\t \t \t簡稱 \t編程\t FOP 我想在輸出添加此記錄,但我有這個寫代碼問題的基礎找到它。 – ish