問題は、キーワード「ドッジ」がファイルを作成するが、それに書き込むことはないということです。私はこの問題を早期に受けていましたが、ファイルを閉じるとフラッシングしてしまいましたが、今回はうまくいきませんでした。デッドシンプルだが長時間の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つの文字列を書いていていることが分かりました。どのように私はファイルを削除し、それを書き直すことはできません?
ああ、ありがとうございます。何らかの理由で、PrintWriterを使用してファイルに書き込まれることが予想される情報の1つの小さな部分、非常に小さな部分が発生しました。 – Malaken