2017-02-11 14 views
1

我有一個簡單的程序,讀取命令並執行它們。 RightNow公司我插入某些文本到文本文件中的代碼:的FileWriter沒有寫入到目錄中適當

示例命令:

INSERT "John Smith" INTO college.student 

我的主要方法:

else if(command.substring(0,6).equalsIgnoreCase("INSERT")){ 
     String string = command.substring(7, command.indexOf("INTO") - 1); 
     String DBNameTBName = command.substring(command.indexOf("INTO") + 5); 
     String tableName = DBNameTBName.substring(DBNameTBName.indexOf(".") + 1); 
     String DBName = DBNameTBName.substring(0, DBNameTBName.indexOf(".")); 

     if(DBCommands.insert(string, DBName, tableName)){ 
      statfileWriter.println("Inserted " + string + " into table " + tableName + " in " + DBName); 
      statfileWriter.println("(" + command + ")"); 
      statfileWriter.flush(); 
     } 
     else{ 
      errfileWriter.println("Error: Could not insert " + string + " into table " + tableName + " in " + DBName); 
      errfileWriter.println("(" + command + ")"); 
      errfileWriter.flush(); 
     }  

並調用insert方法:

public static boolean insert(String string, String DBName, String tableName){ 
    try{ 
     string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); //removes quotes 

     File tableToWriteTo = new File(DBName + "/" + tableName + ".txt"); 
     if (!tableToWriteTo.exists()){ 
      return false; 
     } 

     PrintWriter writer = new PrintWriter(new FileWriter 
        (tableToWriteTo, true)); 
     writer.println(string); 
     writer.close(); 
     return true; 
    } 
    catch(Exception e){ 

     return false; 
    } 

} 

我正在與我的insert方法很怪異的行爲。它返回true,因爲它總是打印到我的狀態日誌而不是錯誤日誌。我知道創建.txt文件的方法工作正常,我已經測試了很多次,並且student.txt文件始終存在。隨着我的插入命令,如果我更改文件=新的文件行這樣的:

File tableToWriteTo = new File(tableName + ".txt"); 

然後不出所料創建一個名爲「學生」與我的例子命令.txt文件,而不是在「數據庫名」文件夾中。如果我把它改成這樣:

File tableToWriteTo = new File(DBName + "/" + tableName); 

然後,它創建了一個名爲「學生」沒有類型的文件(如,Windows要求我要打開它是什麼),但在字符串中把我想要插入到它。我應該注意到,如果有多個INSERT命令,那麼它會按照我的意願寫入所有字符串。

我試圖在我的主要方法聲明PrintWriterFile並通過他們,但這並不能工作。

我怎樣才能得到它寫入目錄中的大學students.txt?

編輯:哦,我的上帝,我是地球上最愚蠢的人。我沒有看到我爲這個任務收到的​​完整命令列表,我忘記了有一個刪除命令,他們都在工作。我會刪除這個問題,但我會留下來,以防將來有人想看到FileWriter的例子。

回答

1

我改變了insert方法中的if條件。該文件不應該存在。所以理想狀況不應該被否定。我使用了下面的代碼,它爲我工作。

public class InsertToWriteTo { 

    public static void main(String[] args) { 
     boolean ret = insert("\"hello\"", "college", "student"); 
     System.out.println(ret); 
    } 

    public static boolean insert(String string, String DBName, String tableName) { 
     try { 
      string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); // removes quotes 

      File tableToWriteTo = new File(DBName + "/" + tableName + ".txt"); 
      if (tableToWriteTo.exists()) { // changed condition 
       System.out.println("File exists"); 
       return false; 
      } 

      PrintWriter writer = new PrintWriter(new FileWriter(tableToWriteTo, true)); 
      writer.println(string); 
      writer.close(); 
      return true; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 

    } 
} 

希望這有助於!

+0

謝謝,但事實證明,我只是愚蠢的。我沒有看完這個任務的完整命令列表,並沒有注意到有一個DELETE命令進一步下去,我已經放入了這個方法,而且他們實際上都在工作。欣賞努力,謝謝 – Johnny