使用JFileChooser保存对话框保存文件 [英] save file with JFileChooser save dialog

查看:127
本文介绍了使用JFileChooser保存对话框保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Java程序,用JFileChooser打开所有类型的文件。然后我想用JFileChooser保存对话框将它保存在另一个目录中,但它只保存一个空文件。我可以做些什么来保存部分?

I have written a Java program that opens all kind of files with a JFileChooser. Then I want to save it in another directory with the JFileChooser save dialog, but it only saves an empty file. What can I do for saving part?

谢谢。

推荐答案

JFileChooser只返回File对象,你必须打开一个FileWriter并实际写入内容。

JFileChooser just returns the File object, you'll have to open a FileWriter and actually write the contents to it.

例如

if (returnVal == JFileChooser.APPROVE_OPTION) {
   File file = fc.getSelectedFile();
   FileWriter fw = new FileWriter(file);
   fw.write(contents);
   // etc...
} 

编辑:

假设您只是拥有一个源文件和目标文件,并希望在两者之间复制内容,我建议使用类似 FileUtils Commons IO 来做繁重的工作。

Assuming that you simply have a source file and destination file and want to copy the contents between the two, I'd recommend using something like FileUtils from Apache's Commons IO to do the heavy lifting.

例如

FileUtils.copy(source, dest);

完成!

这篇关于使用JFileChooser保存对话框保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆