2010-12-05 7 views
1

問題は、キーワード「ドッジ」がファイルを作成するが、それに書き込むことはないということです。私はこの問題を早期に受けていましたが、ファイルを閉じるとフラッシングしてしまいましたが、今回はうまくいきませんでした。デッドシンプルだが長時間のJava PrintWriterの問題

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);また、下部に向かってエラーが出て、ArrayIndexOutOfBoundsException: -2と表示されます。理由は、「使用する」が-1の位置に決して見つからないためです。

import java.io.*;  
import javax.swing.JOptionPane; 

public class InputLog { 
    private BufferedReader CombatLog; 
    private PrintWriter CharacterFile; 
    private String[] CurrentLine; 

    InputLog(){ 
     try{ 
      CombatLog = new BufferedReader(new FileReader("127401175162_chatlog.txt")); 
      do{ 
       try{ 
        CurrentLine = CombatLog.readLine().split(" "); 
       } 
       catch(IOException e){ 
        JOptionPane.showMessageDialog(null, "Sorry cannot open UserSettings File"); 
       } 
       DetermineType(); 
       }while(CombatLog.ready()); 
      CharacterFile.close(); 
     } 
     catch(IOException e){ 
      JOptionPane.showMessageDialog(null, "Could not open next line of combatlog " + e); 
     } 
    } 
    public void WriteToFile(String S){ 
     try{ 
      CharacterFile = new PrintWriter(new File(CurrentLine[3])); 
     } 
     catch(IOException e){ 
     JOptionPane.showMessageDialog(null, "Sorry can't write " + CurrentLine[3] +" To file"); 
     } 
     CharacterFile.flush(); 
     CharacterFile.print(S+ " "); 
    } 

    public void WriteToFileLn(String S){ 
     try{ 
      CharacterFile = new PrintWriter(new File(CurrentLine[3])); 
     } 
     catch(IOException e){ 
      JOptionPane.showMessageDialog(null, "Sorry can't write " + CurrentLine[3] +" To file"); 
     } 
     CharacterFile.flush(); 
     CharacterFile.println(S+ " "); 
    } 


    public int ReturnWordsIndex(String S){ 
     for(int i=0;i<CurrentLine.length;i++){ 
      if(CurrentLine[i].equals(S)) 
       return i; 
     } 
     return -1; 
    } 

    private void DetermineType(){ 
     for(String A: CurrentLine) 
      if(A.equals("attacks")){ 
       JOptionPane.showMessageDialog(null, "found dodge"); 
       WriteToFile(CurrentLine[2]); 
       WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]); 
       WriteToFile("(dodge)."); 
       WriteToFileLn(CurrentLine[ReturnWordsIndex("attacks")-1]); 
      } 
    } 


    public static void main(String args[]){ 
     new InputLog(); 
    } 
} 

おかげで、マケール

編集:私は、私は単に、別の文字を書いて、新しいファイルを作成し、1つの文字列を書いていていることが分かりました。どのように私はファイルを削除し、それを書き直すことはできません?

答えて

0

どうすればファイルを削除できないのですか?

には、が追加されているものとします。

もしそうならば、FileWriter(file, true)を使ってWriterを作成すれば、ファイルの現在の末尾に書き出しを開始します。例えば

CharacterFile = new PrintWriter(new FileWriter(new File(CurrentLine[3]))); 

注:それはJavaの変数やメソッド名は大文字で開始するための悪いスタイルです。

0

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);は、「使用」という単語が見つからないため(ReturnWordsIndex("using")が-1を返す)、ArrayIndexOutOfBoundsException: -2となります。

単語が見つからない場合は、CurrentLineというインデックスを作成しないように、if/then文が必要です。

+0

ああ、ありがとうございます。何らかの理由で、PrintWriterを使用してファイルに書き込まれることが予想される情報の1つの小さな部分、非常に小さな部分が発生しました。 – Malaken

0

あなたの編集に応じて、WriteToFileWriteToFileLn機能以外のファイルを開きます。

関連する問題