aspose file tools*
The moose likes Beginning Java and the fly likes Directory not writable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Directory not writable" Watch "Directory not writable" New topic
Author

Directory not writable

Vikram Gosain
Greenhorn

Joined: Jun 02, 2009
Posts: 25
Hi
In the code below I am getting "The directory is not writable" mesasge.But this directory is writable.I can very much create a file/folder/shortcut in this directory.




Can anyone please help me with this.?.

Thanks
-Vikz


-Vikz
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

Why are you mixing single slashes and double slashes in the directory name?

Windows works with a backslash \ as the path separator, not with a forward slash /. Because \ is an escape character in Java string literals, you have to use \\ in a Java string literal for a backslash. Try this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vikram Gosain
Greenhorn

Joined: Jun 02, 2009
Posts: 25
Jesper Young wrote:Why are you mixing single slashes and double slashes in the directory name?

Windows works with a backslash \ as the path separator, not with a forward slash /. Because \ is an escape character in Java string literals, you have to use \\ in a Java string literal for a backslash. Try this:


Hello Jesper
I tried giving backslash the way you told me.I still did not work.You can try this small piece of code yourself on your system.

Thanks
-Vikz
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3218
    
  49

Are you sure that the given path, (including the "Quick Launch" sub-directory) already exists?

There may be another reason that you are using resources in "Documents and Settings" directory, which may require sufficient privileges to read/write them.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

I think, if you do the experiment, you'll find that "canWrite()" returns false for every single directory in your file system. Try it and tell me if I'm wrong.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3218
    
  49

Paul Clapham wrote:I think, if you do the experiment, you'll find that "canWrite()" returns false for every single directory in your file system. Try it and tell me if I'm wrong.


Mmm, that's not.
It returns true even for directories.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Devaka Cooray wrote:Are you sure that the given path, (including the "Quick Launch" sub-directory) already exists?

Try using File.exists() to make sure your path is correct.


SCJP 6 || SCWCD 5
cchetan jain
Ranch Hand

Joined: Jul 05, 2009
Posts: 30
hi..when this directory exists in the path,then canWrite() is true..and if this directory is not exists,it is simply can not be written obviously because after the instance creation of File object,the file is not created actually if the file is not exist.so canWrite() becomes false in this case..i tried it on my pc..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Devaka Cooray wrote:
Paul Clapham wrote:I think, if you do the experiment, you'll find that "canWrite()" returns false for every single directory in your file system. Try it and tell me if I'm wrong.


Mmm, that's not.
It returns true even for directories.

Okay. And when you did the experiment, did you notice anything the the directories with canWrite=false had in common?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Jesper Young wrote:Windows works with a backslash \ as the path separator, not with a forward slash /.

Actually, File knows how to handle forward slashes in Windows as well. That's why I prefer to use only forward slashes when creating relative File objects.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

If you really want to do it right, you should use File.separator instead of putting \ or / characters directly in your string:

But that's kind of long, and if the path starts with "C:", which is a Windows-only thing, it doesn't really make sense...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Directory not writable