Java 为什么不是';我的文件没有被追加吗?

Java 为什么不是';我的文件没有被追加吗?,java,windowbuilder,Java,Windowbuilder,嗯,我学习了用Java创建和写入文件。但是我不明白为什么我的输出不会写入,我的文件找不到,因为它是在我的主方法中创建/读取的,请注意,它在同一个类中 public class EloManip { private JFrame frame; private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextF

嗯,我学习了用Java创建和写入文件。但是我不明白为什么我的输出不会写入,我的
文件
找不到,因为它是在我的主方法中创建/读取的,请注意,它在同一个
类中

public class EloManip {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JLabel lblFormat;
    private JTextField textField_5;
    private JLabel lblCase;
    private JLabel label;

    //Path p5 = Paths.get(System.getProperty("user.home"),"EloManip", "EloManipCases.log");

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {

            String content = "This is the content to write into file";

            File file = new File("/users/Peter/EloManipulation.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    EloManip window = new EloManip();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public EloManip() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setTitle("Elo Manipulation Case Creator by Cavasi");

        /*lblFormat = new JLabel("Format");
    lblFormat.setBounds(32, 180, 307, 14);
    frame.getContentPane().add(lblFormat);
         */

        textField = new JTextField();
        textField.setBackground(new Color(255, 255, 255));
        textField.setBounds(173, 34, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblEnterName = new JLabel("Enter name:");
        lblEnterName.setBounds(32, 37, 122, 14);
        frame.getContentPane().add(lblEnterName);

        JLabel lblEnterReason = new JLabel("Enter Reason:");
        lblEnterReason.setBounds(32, 62, 104, 14);
        frame.getContentPane().add(lblEnterReason);

        textField_1 = new JTextField();
        textField_1.setBackground(new Color(255, 255, 255));
        textField_1.setBounds(173, 59, 86, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        JLabel lblEnterEvidence = new JLabel("Enter Evidence:");
        lblEnterEvidence.setBounds(32, 87, 131, 14);
        frame.getContentPane().add(lblEnterEvidence);

        textField_2 = new JTextField();
        textField_2.setBackground(new Color(255, 255, 255));
        textField_2.setBounds(173, 84, 86, 20);
        frame.getContentPane().add(textField_2);
        textField_2.setColumns(10);

        JLabel lblEnterDatemmddyy = new JLabel("Enter Date(MM/DD/YY):");
        lblEnterDatemmddyy.setBounds(32, 112, 136, 14);
        frame.getContentPane().add(lblEnterDatemmddyy);

        textField_3 = new JTextField();
        textField_3.setBackground(new Color(255, 255, 255));
        textField_3.setBounds(173, 109, 86, 20);
        frame.getContentPane().add(textField_3);
        textField_3.setColumns(10);

        JLabel lblEnterLength = new JLabel("Enter Length:");
        lblEnterLength.setBounds(32, 137, 122, 14);
        frame.getContentPane().add(lblEnterLength);

        textField_4 = new JTextField();
        textField_4.setBackground(new Color(255, 255, 255));
        textField_4.setBounds(173, 134, 86, 20);
        frame.getContentPane().add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setBackground(new Color(255, 255, 255));
        textField_5.setBounds(10, 207, 414, 20);
        frame.getContentPane().add(textField_5);
        textField_5.setColumns(10);

        JButton btnNewButton = new JButton("Generate Elo Manipulation Case");
        btnNewButton.setFont(new Font("Nirmala UI Semilight", Font.BOLD, 11));
        btnNewButton.setForeground(Color.BLACK);
        btnNewButton.setBackground(Color.GREEN);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //lblFormat.setText(textField)
                //JOptionPane.showMessageDialog(null, textField.getText() + " - " + textField_1.getText() + " - " + textField_2.getText() + " " + textField_3.getText() + " [" + textField_4.getText() + "]");
                textField_5.setText(textField.getText() + " - " + textField_1.getText() + " - " + textField_2.getText() + " " + textField_3.getText() + " [" + textField_4.getText() + "]");
                JOptionPane.showMessageDialog(null, "Successfully created Elo Manipulation Case!");
                Writer output;
                output = new BufferedWriter(new FileWriter(file, true));  
                output.append("New Line!");
                output.close();
            }
        });
        btnNewButton.setBounds(0, 238, 434, 23);
        frame.getContentPane().add(btnNewButton);

        lblCase = new JLabel("Case:");
        lblCase.setBounds(10, 182, 352, 14);
        frame.getContentPane().add(lblCase);

        label = new JLabel("");
        label.setBounds(269, 11, 155, 163);
        frame.getContentPane().add(label);

        Image img = new ImageIcon(this.getClass().getResource("/badlionsmall.png")).getImage();

        label.setIcon(new ImageIcon(img));
    }
}
无法将文件解析为变量,因为它正在我的主方法中创建/检查,但需要写入
.txt
文件的操作不在主方法中,而是在initialize方法中。我该怎么办

为什么没有附加我的文件

如果我错了,请纠正我。。。但我认为这个问题实际上是关于一个编译错误。问题是
main
方法中的局部变量
file
不在侦听器的作用域内

解决方案是移动
文件
的声明。最简单的方法是使其成为
EloManip
的静态(私有、最终)字段


(将状态放入静态通常不是一个好主意,但是如果状态实际上是一个常量,并且它被本地化为单个类及其内部类,则没有太大的危害。但是,有一种更面向对象的方法来处理此问题……

您实际上声明了两个FileWriter变量

  FileWriter fw = new FileWriter(file.getAbsoluteFile());
  BufferedWriter bw = new BufferedWriter(fw);
  Writer output;
  output = new BufferedWriter(new FileWriter(file, true));  
你试过只用一个吗?我怀疑第一个是在清除你的内容,而不是第二个。

这对我很有效

public static void main(String[] args) throws IOException {
    File f = new File("test.txt");
    try(FileWriter fw = new FileWriter(f, true)) {
        fw.append("charsequence\r\n");
    } catch(IOException e) {
        e.printStackTrace();
    }
}

请记住,对文件的更改只有在您关闭它然后再次打开它时才会显示。

局部变量的作用域不是它们出现在其中的方法的类,而是它们出现在中的大括号(
{}
)。如果您希望能够在类中的任何位置使用变量,则需要将其设置为类变量。如果您有新问题,请提出新问题。不要覆盖这篇文章。正如很多次写的那样,一个新问题属于一篇新文章,而不是附加到这篇文章中。你可以通过点击右上角的“提问”来创建一个新问题。我已经解决了这个问题,但是现在我的文本被覆盖了。一旦我点击我的按钮,现在检查我的线程。你通过删除我的答案所指的问题来“破坏”你的问题。如果您想让我回答您的后续问题,请将原始代码放回原处,并以编辑或新问题的形式询问您的后续问题。从恢复上次编辑开始…@Cavasi请不要更改您的问题。它会使答案无效。“请记住,只有关闭该文件,然后再次打开它,才会显示对该文件的更改。”-不正确。
flush()
也可以工作,您不必再次打开文件(在此应用程序中),更改就会显示在另一个应用程序中。如果他在刷新流之前在记事本之类的程序中打开文件,则不,更改将不可见。这是正确的。如果他在刷新或关闭之前在另一个程序中打开它。但是他不需要再打开它。我只是假设OP使用了记事本,因为我的示例(它是OPs源代码的收缩版本)实际上可以工作,但他看不到这些更改:)