简单但冗长的 Java PrintWriter 问题

发布于 2024-10-05 19:07:13 字数 2588 浏览 0 评论 0原文

问题是它发现关键字“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪少女 2024-10-12 19:07:13

如何才能不删除该文件,而只是重写它?

我假设您的意思是附加到它。 (您当前正在做的是重写它。)

如果是这样,只需使用 FileWriter(file, true) 创建一个 Writer ,它将在文件的当前结尾;例如

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

注意:Java 变量或方法名称以大写字母开头是不好的风格。

How can i not delete the file, and just rewrite to it?

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 a Writer that will start writing at the current end of the file; e.g.

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

Note: it is bad style for a Java variable or method name to start with an upper-case letter.

泼猴你往哪里跑 2024-10-12 19:07:13

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]); 会导致 ArrayIndexOutOfBoundsException: -2 因为找不到单词“using”(因此 ReturnWordsIndex (“使用”) 返回-1)。

您需要一个 if/then 语句,以便在未找到该单词时不会尝试对 CurrentLine 建立索引。

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]); causes an ArrayIndexOutOfBoundsException: -2 because the word "using" is not found (so ReturnWordsIndex("using") returns -1).

You need an if/then statement so that you don't try to index CurrentLine if the word wasn't found.

放血 2024-10-12 19:07:13

为了响应您的编辑,请在 WriteToFileWriteToFileLn 函数之外打开文件。

In response to your edit, open the file outside of the WriteToFile and WriteToFileLn functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文