1

問題は、「dodge」というキーワードがファイルを作成するのに書き込みを行わないことを検出することです。以前にこの問題があり、フラッシュしてからファイルを閉じると修正されましたが、今回はうまくいきませんでした。

また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つの文字列を書き、新しいファイルを作成してから別の文字を書いていることがわかりました。どうすればファイルを削除せずに書き換えることができますか?

4

3 に答える 3

0

編集に応じて、WriteToFileandWriteToFileLn関数の外でファイルを開きます。

于 2010-12-05T23:15:22.900 に答える
0

ファイルを削除せず、単に書き換えるにはどうすればよいですか?

私はあなたがそれに追加することを意味すると思います。(現在行っているのは書き直しです。)

その場合は、ファイルの現在の終わりから書き込みを開始FileWriter(file, true)するを作成するために使用します。Writer例えば

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

注:Java変数またはメソッド名を大文字で始めるのは不適切なスタイルです。

于 2010-12-05T23:32:23.507 に答える
0

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);ArrayIndexOutOfBoundsException: -2"using" という単語が見つからないため、 が発生します (つまり、-1ReturnWordsIndex("using")が返されます)。

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

于 2010-12-05T19:09:58.773 に答える