2013年5月3日金曜日

隠しファイルに対してFileOutputStream.open() でFileNotFoundException例外

隠しファイルに対してFileOutputStreamのopenメソッドでFileNotFoundExceptionが発生する。
jdk-b63で解決しているのだが、一応念のため書いておきます。

http://bugs.sun.com/view_bug.do?bug_id=6350200


Exception in thread "main" java.io.FileNotFoundException: sample.txt (アクセスが拒否されました。)
       at java.io.FileOutputStream.open(Native Method)
       at java.io.FileOutputStream.(FileOutputStream.java:179)
       at java.io.FileOutputStream.(FileOutputStream.java:131)
       at java.io.FileWriter.(FileWriter.java:73)
       at sample.FileAccessSample.main(FileAccessSample.java:22)


以下のようなコードで発生する。


import java.io.*;

public class HiddenFileTest {
    // Main function, launches JFileChooser to select a directory
    public static void main( String[] args ) {
        try {
            FileWriter fw = new FileWriter( "C:\\temp\\file1.txt" );
            PrintWriter out = new PrintWriter( fw );
            out.println( "This is a test 2" );
            out.close();
        } catch( IOException e ) {
            e.printStackTrace( System.out );
        }
    }

}