This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File object can't find directory  RSS feed

 
Paula Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am actually having 2 issues which might or might not be related to each other. I have a Java class in /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11/dir12
that needs to write to a file located in /dir1/dir2/dir3/dir4/dir5/dirA/dirB
So I'm starting out with:
File logFile = new File("/dir1/dir2/dir3/dir4/dir5/dirA/dirB/test.txt");
then checking the value of logFile.exists() (The file should always exist because I created it manually prior to running the program.)
If logFile.exists() == true, then I'm constructing a FileWriter and writing some stuff to the file.
First attempt gave logFile.exists() == false.
Then I moved the text file into the same directory where the Java class lives, and used
File logFile = new File("test.txt");
This gave me logFile.exists() == true, and the program was able to write to the file also. Then I started moving the text file up the tree, one level at a time. I got logFile.exists() == true all the way to one directory above the target:
File logFile = new File("/dir1/dir2/dir3/dir4/dir5/dirA/test.txt"); // still OK, logFile.exists() == true
But when I added in that last directory level (dirB) then I got logFile.exists() == false.
I also tried
File logFile = new File("../../../../../../../dirA/dirB/test.txt");
to no avail.
Any idea what I should look for about dirB that can be causing this? I checked the obvious things such as spelling of names, no levels omitted from path, etc.
My second question is about writing to the file when it is found. (I'm not even sure whether this is *really* a Java question or a UNIX question, so if it's not a Java question, somebody tell me.) When I had my text file in the same directory with the Java class, the write operation succeeded. As soon as I moved it out of there, I started getting "permission denied" even when both the file and the directory it lived in had permissions set for the whole world to be able to write as well as read. I am wondering whether Java ever says "permission denied" for other reasons besides what *I* would recognize as permissions being set wrong in the OS? Here's code:

Thanks in advance.
[ March 26, 2004: Message edited by: Paula Davis ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java class in
Since nobody has an answer, I started interesting in this topic.
Once I counted the ('..') up-dirs and thought I found it, but I was wrong, the number seems to be correct.
I thought you might lack write-permissions for the directory, so you could not CREATE a new file - but you say you created the file before.

You say you may not write to the file, but does your 'if'- statement refuse you to write, or is an Exception thrown?
Now I made a small test on my system, and found out, that I had to use one more '../' than thought.
curious!
Can't you come up with an easier problem
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An additional small idea:
Then I moved the text file into the same directory where the Java class lives...

You know, that the location of a class isn't important, but the location, from where you invoke it?
Only to be sure...
 
Paula Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WELL, it would appear that my two problems are one and the same, and they are a UNIX thing: based on additional trial and error (OK, mainly lucky accident) it looks like whoever/whatever wants to write to the file needs to have not only write permission for the file and the directory where it lives, but also execute permission for the directory.
This is what happens when a Windows girl ventures into UNIX-land without adequate advance enculturation...
But it looks like I might be good to go on this now
Thanks, Stefan. Following up on your replies led to this fortunate discovery.
[ March 31, 2004: Message edited by: Paula Davis ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry that I didn't tried this, because I thought it might be the source of troubles, but then I thought: If you could create that file in that directory, and since you went up directory by directory, you will have execution permission.
So I didn't mention.
And I wasn't sure myself, whether you need execution-permission for dir2, if you try to write to:
/dir1/dir2/dir3/file
In some way you need execution permission to 'enter' that directory.
And that's not true for Windows?
Or is it only seldom happening, since almost nobody cares on win-platform, for directory-permissions?
However - welcome to Unix-Land and dirB.
 
Paula Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, now that you mention it, my problem could be that this is my first time in server-land too, so it's possible that I also would have had a similar problem on a Windows server. Not sure. All I know is that in my small little PC world before now, I never ran into permission troubles with Java I/O. But maybe that wasn't b/c Windows but because I was all there was on *my* machine, so I just "happened" to have the permissions I needed.
Anyway, thanks for your help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!