"dodge"라는 키워드가 파일을 생성하지만 쓰기는하지 않는다는 것이 문제입니다. 나는이 문제를 더 일찍 겪었고 파일을 닫으면 그 문제가 해결되었지만 이번에는 작동하지 않았습니다.죽은 단순하지만 긴 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();
}
}
감사합니다, Macaire
편집 : 나는 간단히 다른 문자를 쓰고, 새로운 파일을 생성 한 문자열을 쓰고 있다는 것을 발견했다. 어떻게 파일을 지우지 않고 파일에 다시 쓸 수 있습니까?
아 감사합니다. 웬일인지, PrintWriter를 사용하여 파일에 기록 될 정보의 아주 작은 부분 인 작은 조각 1 개가 발생했습니다. – Malaken