Java 正在尝试将数据发送到文本文件

Java 正在尝试将数据发送到文本文件,java,jbutton,jtextfield,Java,Jbutton,Jtextfield,大家好,我制作了一个由jtextfield和两个jbutton组成的程序。我想按一个jbutton,这样jtextfields就会保存到计算机上。任何帮助都是有用的。我想这会对你有所帮助 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (jTxt_text.getText().isEmpty())

大家好,我制作了一个由jtextfield和两个jbutton组成的程序。我想按一个jbutton,这样jtextfields就会保存到计算机上。任何帮助都是有用的。

我想这会对你有所帮助

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    if (jTxt_text.getText().isEmpty()) {
        JOptionPane.showMessageDialog(rootPane, "Field is empty. Fill the filed and try again.");
    } else {
        //get the text from the jTextField and save it into a varibale. 
        String inputText = jTxt_text.getText();
        //Where to save the file.
        String savePath = "C:/test/sample.txt";

        //Creating a file object, file is an abstract representation of file and directory pathnames.
        File tempFile = new File(savePath);

        //Check wther the file is available or not.
        if (!tempFile.exists()) {
            try {
                //Creates the file if it's not exsising.
                tempFile.createNewFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        try {
            //writing process..
            FileWriter tempWriter = new FileWriter(tempFile.getAbsoluteFile());
            BufferedWriter tempBufferWriter = new BufferedWriter(tempWriter);
            tempBufferWriter.write(inputText);
            tempBufferWriter.close();

            JOptionPane.showMessageDialog(rootPane, "Text file with the written text is successfully saved.");
            jTxt_text.setText(null);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}  

这段代码仍然存在一个小问题tempBufferWriter.writeinputText返回void,所以。。我不知道如何从代码本身检查进程是否成功完成

你为什么不查一下关于将数据保存到文件的教程呢?它们是丰富的。到目前为止,你已经尝试了哪些?那么你的问题是什么?您知道如何进行文件I/O吗?您知道在单击按钮时如何执行代码吗?您知道如何从文本字段中获取文本吗?问问题时要具体。我们猜不出是什么导致了你的问题。从Java教程开始。这里有一个关于I/O和使用Swing的教程,可以回答您的所有问题。我知道如何使按钮工作。我有一个文本字段,但不知道如何将其发送到文本文件。我有许多文本字段要同时写入。15确切地说,它们不一定都是字段。因此,使用此代码应该可以编写所有这些内容?这是一个基本代码,您可以在其中获取jTextField的值并将其写入文本文件。。。可能有jTextAreas、jComboxes、jRadioButtons,所以您只需从这些组件中获取所有值,然后保存到变量中并构建一个单独的字符串。并将其作为write方法的参数传递。