2017-02-11 3 views
1

私はコマンドを読み込んで実行する簡単なプログラムを持っています。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(); 
     }  

そして、それは呼び出しを挿入する方法:

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; 
    } 

} 
のRightNowは、私は、テキストファイルに特定のテキストを挿入するため、このコードを持っています

私の挿入メソッドでは非常に奇妙な動作をしています。常にステータスログに出力され、エラーログは出力されません。私は.txtファイルを作成する方法が完全に動作していることを知っています。私は何度もそれをテストしました。そして、student.txtファイルは常にそこにあります。私はこれに、ファイル=新しいファイルの行を変更した場合、私の挿入コマンドを使用すると、:

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

そして、それは当然私の例のコマンドではなく、「DBNAME」フォルダ内の「学生」と呼ばれる.txtファイルを作成します。私はこのように変更した場合:

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

を(、Windowsは私がそれを開くようにしたいものを尋ねるのように)が、私は挿入したい文字列を入れそれからそれは無いタイプの「学生」というファイルを作成しますそれ。複数のINSERTコマンドがある場合は、すべての文字列を書き込みたいと思います。

私の主な方法でPrintWriterFileを宣言しようとしていますが、それもうまくいきません。

ディレクトリカレッジのstudents.txtに書き込むにはどうすればよいですか?

EDIT:ああ、私は地球上で最も愚かな人です。私はこの任務のために受け取った完全なコマンドリストを見ていないし、削除コマンドがあったことを忘れていて、彼らは両方とも作業していた。私はこの質問を削除しますが、今後誰かが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

ありがとうございましたが、わかりました。私はこの割り当ての完全なコマンドリストを見ていないし、さらに削除コマンドがあったことに気付かず、すでにメソッドに入れていて、実際には両方とも動作していました。しかし、努力を感謝、ありがとう – Johnny

関連する問題