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)
我有办法解决这个问题吗?
答案 0 :(得分:0)
看起来像一个bug,在我的系统上测试和确认以及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)
一个丑陋的解决方法可能是指定一个过滤掉那些的文件过滤器,请参阅:
Selecting 'Computer' or 'Libraries' in Java's JFileChooser yields a strange File object