简单但冗长的 Java PrintWriter 问题
问题是它发现关键字“dodge”创建文件但不写入它。我之前遇到过这个问题,我冲洗然后关闭文件修复了它,但这次不起作用。
另外 WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);
朝向底部错误并显示 ArrayIndexOutOfBoundsException: -2
我不知道为什么,因为“using " 永远不应在位置 -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
编辑:我发现我只是编写一个字符串,创建一个新文件,然后编写另一个字符。我怎样才能不删除该文件,而只是重写它?
The problem is it finds the key word "dodge" creates the file but doesn't write to it. I had this problem earlier and I flushing then closing the file fixed it, but that did not work this time.
Also WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);
towards the bottom errors out and says ArrayIndexOutOfBoundsException: -2
I'm not sure why because "using" should never be found at position -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();
}
}
Thanks, Macaire
Edit: I found out that i am simply writing one string, creating a new file, then writing another character. How can i not delete the file, and just rewrite to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您的意思是附加到它。 (您当前正在做的是重写它。)
如果是这样,只需使用
FileWriter(file, true)
创建一个Writer
,它将在文件的当前结尾;例如注意:Java 变量或方法名称以大写字母开头是不好的风格。
I assume that you mean append to it. (What you are currently doing is rewriting it.)
If so, just use
FileWriter(file, true)
to create aWriter
that will start writing at the current end of the file; e.g.Note: it is bad style for a Java variable or method name to start with an upper-case letter.
WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);
会导致ArrayIndexOutOfBoundsException: -2
因为找不到单词“using”(因此ReturnWordsIndex (“使用”)
返回-1)。您需要一个 if/then 语句,以便在未找到该单词时不会尝试对
CurrentLine
建立索引。WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);
causes anArrayIndexOutOfBoundsException: -2
because the word "using" is not found (soReturnWordsIndex("using")
returns -1).You need an if/then statement so that you don't try to index
CurrentLine
if the word wasn't found.为了响应您的编辑,请在
WriteToFile
和WriteToFileLn
函数之外打开文件。In response to your edit, open the file outside of the
WriteToFile
andWriteToFileLn
functions.