United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4939819 File.canWrite() returns false for the "My Documents" directory (win)
4939819 : File.canWrite() returns false for the "My Documents" directory (win)

Details
Type:
Bug
Submit Date:
2003-10-17
Status:
Closed
Updated Date:
2010-05-24
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
core-libs
OS:
windows_vista,windows_2008,windows_xp
Sub-Component:
java.io
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.2,6u10,6u16
Fixed Versions:
7

Related Reports
Backport:
Backport:
Backport:
Duplicate:
Duplicate:
Relates:
Relates:
Relates:

Sub Tasks

Description
Name: rmT116609			Date: 10/17/2003


FULL PRODUCT VERSION :
java version "1.4.2_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06)
Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)

FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
File.canWrite() returns false for the user's "My Documents" directory, when specified as the path "c:\documents and settings\<username>\my documents".

This is incorrect as the "My Documents" directory is writable.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the test program below.  When the Swing file chooser appears, navigate to your "My Documents" directory via
c:\documents and settings\<your username>\my documents
Click OK.  The debug output will indicate that this directory is not writable.
Some subdirectories under "My Documents" are writable - here's some sample output:
$ javac Foo.java && java Foo
** Not writable **: C:\Documents and Settings\johnc\My Documents
Writable: C:\Documents and Settings\johnc\My Documents\Visual Studio Projects
Writable: H:
Note, this is not a Swing problem, as the same happens when creating a File object for "c:\documents and settings\johnc\my documents" and calling canWrite() on that.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
canWrite() should return true for the "My Documents" directory.
ACTUAL -
canWrite() returns false for the "My Documents" directory.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.io.File;

public class Foo extends JFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setFileHidingEnabled(false);

        while (true) {
            int option = chooser.showDialog(frame, "OK");
            if (option != JFileChooser.APPROVE_OPTION)
                continue;

            File file = chooser.getSelectedFile();

//         try {
//             file = file.getAbsoluteFile();
//             file = file.getCanonicalFile();
//         } catch (Exception e) { System.out.println("e"); }

            if (file.canWrite()) {
                System.out.println("Writable: "+file);
            } else {
                System.out.println("** Not writable **: "+file);
            }
            chooser.setCurrentDirectory(file);
        }
    }
}

---------- END SOURCE ----------
(Incident Review ID: 216229) 
======================================================================

                                    

Comments
EVALUATION

On my XP system, right-clicking on ANY folder and showing properties shows the folder as being read-only. But only "My Documents" disallows writing. The attrib command shows the R flag for "My Documents" but not for other folders.
                                     
2006-04-25
WORK AROUND

Make the dierctory writable by toggle the readonly attribute (eg: attrib -r C:\"Documents and Settings"\Alice\"My Documents")
                                     
2006-03-08
EVALUATION

--

If you right-click on C:\Documents and Setting\Alice\My Documents and examine the properties you will see that the Read-only attribute bit is set. The java.io.File canWrite method tests for write access using the Windows _waccess function and indicates that write permission is not allowed. At this time java.io.File does not understand  special shell folders and the only workaround is to toggle the readonly bit and make the directory writable.
                                     
2006-03-08
EVALUATION

Will investigate after build 30.
###@###.### 2003-11-18
                                     
2003-11-18



Hardware and Software, Engineered to Work Together