在文本文件中写入文本并使用 saveDialog 保存

发布于 2024-08-03 00:15:47 字数 1234 浏览 1 评论 0原文

我想在文本文件中写入一个字符串,该字符串应该由保存对话框动态保存。 我已静态完成此任务,这意味着将创建具有指定文件名的文件,并且还会从 JTextArea 写入文本。我想将此文件保存在我给定的位置并使用我的给定名称。您能在这方面指导我吗?

do{
    String fileData=jTextArea1.getText();
    byte buf[]=fileData.getBytes();

    JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Text/fasta files", ".txt", ".fasta");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showSaveDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("inside try after retVal");
                    try{
                        //OutputStream f2=new FileOutputStream("filename.txt");
                        OutputStream f2=new FileOutputStream("file.txt");

                        f2.write(buf);
                        f2.close();
                        } catch (IOException ex) {
                            Logger.getLogger(CreatingFile.class.getName()).log(Level.SEVERE, null, ex);
                                                }


                } else {
                    return null;
                } //else ends


    // TODO add your handling code here:
}while(true);

I want to write a string in a text file which should be dynamically saved by the save dialog.
I have done this task statically, meaning the file with the specified file name is created and text is also written from JTextArea. I want to save this file on my given location and with my given name. Can you please guide me in this regard?

do{
    String fileData=jTextArea1.getText();
    byte buf[]=fileData.getBytes();

    JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Text/fasta files", ".txt", ".fasta");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showSaveDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("inside try after retVal");
                    try{
                        //OutputStream f2=new FileOutputStream("filename.txt");
                        OutputStream f2=new FileOutputStream("file.txt");

                        f2.write(buf);
                        f2.close();
                        } catch (IOException ex) {
                            Logger.getLogger(CreatingFile.class.getName()).log(Level.SEVERE, null, ex);
                                                }


                } else {
                    return null;
                } //else ends


    // TODO add your handling code here:
}while(true);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

简单爱 2024-08-10 00:15:47

使用 Chooser.getSelectedFile()

使用包装在 BufferedWriter 中的 FileWriter,而不是 FileOutputStream。

并使用 JTextArea 的 write(...) 方法写出文本。

Use chooser.getSelectedFile()

Use a FileWriter, wrapped in a BufferedWriter, not a FileOutputStream.

And use the write(...) method of JTextArea to write out the text.

冬天旳寂寞 2024-08-10 00:15:47

使用

new FileOutputStream(chooser.getName());

而不是

new FileOutputStream("file.txt");

Use

new FileOutputStream(chooser.getName());

instead of

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