1

Windows 以不同于常规文件夹和路径的方式处理库。因此,当我使用这段代码创建一个文本文件时:

File filePath = fc.getSelectedFile();

...更多无关紧要的东西...

File outputText = new File(filePath.getParentFile(), "Decrypted.txt");

    try
        {
        FileWriter fw = new FileWriter(outputText); //Write everything to the file.
        fw.write(messageOut);
        fw.close(); //DON'T FORGET TO CLOSE THE FILE!
        }
    catch (IOException e)
        {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

如果文件在桌面文件夹中,它可以工作,但是如果我尝试将它放在我的图片库中,我会收到以下错误消息:

java.io.FileNotFoundException: ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Pictures.library-ms\Decrypted.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at code.Crypto.decrypt(Crypto.java:57)
at code.Crypto.main(Crypto.java:27)

我有办法解决这个问题吗?

4

1 回答 1

0

看起来像一个错误,在我的系统上以及 java 7 update4 32bit 上进行了测试和确认。

JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(null);
//select and enter a name for a file under libraries (with windows look and feel 
//select 'desktop' in the left pane, then libraries->pictures)
if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    FileWriter fw = new FileWriter(file); //<-- FileNotFoundException
    fw.write("foo bar");
    fw.close();
}

例外:

Exception in thread "main" java.io.FileNotFoundException: ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Pictures.library-ms\hej.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
at java.io.FileWriter.<init>(FileWriter.java:90)
at test.Main.main(Main.java:16)

一个丑陋的解决方法可能是指定一个过滤掉这些的文件过滤器,请参阅:

在 Java 的 JFileChooser 中选择“计算机”或“库”会产生一个奇怪的文件对象

于 2012-05-27T22:53:41.887 回答