如何在Java中使用JFileChooser保存文件? [英] How to save file using JFileChooser in Java?

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

问题描述

我有以下代码。它保存文件,但空的内容。这是什么错误?
$ b $ pre $ public void saveMap(){
String sb =TEST CONTENT;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(/ home / me / Documents));
int retrival = chooser.showSaveDialog(null);
if(retrival == JFileChooser.APPROVE_OPTION){
try {
FileWriter fw = new FileWriter(chooser.getSelectedFile()+。txt);
fw.write(sb.toString());
catch(Exception ex){
ex.printStackTrace();




$ div class =h2_lin>解决方案

如果您使用的是Java 7,请使用资源尝试。这是你将如何做到这一点:$ b​​
$ b $ $ p $ code> try(FileWriter fw = new FileWriter(chooser.getSelectedFile()+.txt )){
fw.write(sb.toString());



$ b

尝试使用资源自动调用 close()

如果您不使用Java 7,请不要忘记调用 close() / code>。 close()会自动调用 flush()

  ... 
fw.close();
...

要理解为什么需要刷新,您需要了解FileWriter的作品。当你说 fw.write(blah),它实际上把这个字符串放到内存中的一个缓冲区中。一旦你填充缓冲区,FileWriter然后将字符串写入硬盘驱动器。它有这样的行为,因为在大块中写文件是非常有效的。



如果你想在缓冲区达到容量之前清空缓冲区,你需要告诉FileWriter通过调用 flush()。调用 flush()在通信时也是非常重要的,比如通过互联网,因为在另一端可以看到你的消息之前需要刷新。如果你的消息只是坐在内存中,它将不会使用太多。



完成任何I / O流后,应该调用 close()(除了标准I / O流)。这意味着操作系统不再需要维护这个流。在某些情况下,可以打开的数据流数量有限,如使用文件,所以不要忘记关闭这一点非常重要。



当你调用close时,它实际上做了两件事:它清空缓冲区,然后关闭流。这是为了确保在流关闭之前没有任何东西被留下。


I have following code. It saves file but with empty content. What's wrong with it?

public void saveMap() {
    String sb = "TEST CONTENT";
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("/home/me/Documents"));
    int retrival = chooser.showSaveDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) {
        try {
            FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt");
            fw.write(sb.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

解决方案

If you're using Java 7, use try with resources. This is how you would do it:

try(FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt")) {
    fw.write(sb.toString());
}

Try with resources automatically calls close() upon failure or success.

If you're not using Java 7, don't forget to call close(). close() will automatically call flush().

...
fw.close();
...

To understand why you need to flush, you need to understand how a FileWriter works. When you say fw.write("blah"), it actually puts that string into a buffer in memory. Once you fill the buffer, the FileWriter then writes the string to the hard drive. It has this behavior because writing files is much more efficient in large chunks.

If you want to empty the buffer before the buffer reaches capacity, you'll need to tell the FileWriter this by calling flush(). Calling flush() can also be very important when communicating, such as over the internet, because you'll need to flush before the other end can see your message. It won't do them much use if your message is just sitting in memory.

Once you're done with any I/O stream, you should call close() (with the exception of the standard I/O streams). This means the OS no longer has to maintain this stream. In some cases, there are a limited number of streams that can be opened, such as with files, so it is extremely important that you don't forget to close.

When you call close, it actually does two things: it empties the buffer and then closes the stream. This is to make sure that nothing gets left behind before the stream closes.

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

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