问题在于它找到了关键词“道奇”创建文件,但没有写入文件。我早些时候遇到了这个问题,然后刷新了文件修复了文件,但这一次无效。

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

谢谢,麦凯尔

编辑:我发现我只是在编写一个字符串,创建一个新文件,然后编写另一个字符。我如何不删除文件,而只是重写它?

有帮助吗?

解决方案

我如何不删除文件,而只是重写它?

我认为你的意思是 附加 对此。 (您目前正在进行的是重写它。)

如果是这样,请使用 FileWriter(file, true) 创建一个 Writer 这将在文件的当前末端开始写作;例如

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

注意:Java变量或方法名称以上案字母开始是不好的样式。

其他提示

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]); 引起 ArrayIndexOutOfBoundsException: -2 因为找不到“使用”一词(所以 ReturnWordsIndex("using") 返回-1)。

您需要一个if/then语句,以免索引 CurrentLine 如果没有找到这个单词。

为了响应您的编辑,打开文件外部的文件 WriteToFileWriteToFileLn 功能。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top