aspose file tools*
The moose likes I/O and Streams and the fly likes Writing to a directory in a Unix environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing to a directory in a Unix environment" Watch "Writing to a directory in a Unix environment" New topic
Author

Writing to a directory in a Unix environment

Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
Hi,

I'm using FileWriter to write to a folder in a Unix environment but when I deploy and run I receive an error saying no such file or directory.
The same code works in the window environment I tested in. is there some special format I need to be using? The directory I'm trying to write to is on the root level and the jar with my code is in another directory about 4 folders deep.
Right now I'm trying :
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

Well, there could be several things going on, but give it a try without the backslashes. Unix doesn't use them as path separators.

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

As Greg mentions, you should not be using a backslash. The correct way is to use the File.separatorChar to determine what the correct separator character is supposed to be - that way you do not have to worry about what operating system you are going to deploy on.

Most Unix environments do not have a /temp directory, however most of them have a /tmp directory. However even this knowledge will not help you if you end up deploying to some other operating system in the future. Once again, the creators of Java thought about these issues, and they provided a File.createTempFile(String prefix, String suffix) that will do the hard work of determining a good location for the temporary file for you.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
btw i originally tried it without the backslashes and it didn't work. I'll try this FileSeparator.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Is there a folder called "temp" in the parent folder? Since you said you were in the root that probably means no; it's called "tmp" as Andrew said.

If you want the system-wide temp folder you can use the results of System.getProperty("java.io.tmpdir").


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
my code uses tmp. i just typed temp in here because i was typing from memory
heres how the files sit:
/tmp/test.log (currently exists)
/otherfolder/1st subfolder/2nd subfolder/3rd subfolder/my jar
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

File.separator on Unix is going to contain "/" anyway, so if it didn't work with explicit slashes, it's not going to work by concatenating in File.separator. There must be some other issue there. It could be permissions, which can be trickier on Unix. That directory (../temp, ../tmp, or /tmp I'm not sure which one you settled on) must exist. If test.log exists, it must be writable for your user. If not, then the directory must be writable to create it. I think the directory would have to be executable in either case.

As a side point, there's no need to ever use File.separator since "/" in Java paths works on every platform, including Windows. You can safely ignore this particular detail of Windows' backwardness.
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
i have the proper permissions when executing the jar. maybe it's a matter of the number of "."'s since I'm calling the class from 4 directories down and the location i want to write to is closer to the root. I've only tried with 2 dots ie
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Maybe I missed something, but if you know that you wish to create the file in the absolute path of "/tmp/test.log", why are you bothering with relative paths "../.."?

Perhaps you should try an explicit path to your desired file, and if that works then you know that the problem is with your attempt to set it relative to your current working directory.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

In writing that, I realized another potential issue as well - you keep talking about the location of your deployed Jar file, when you should really be talking about the current working directory (or, at least, if you want to keep using relative paths to get to a known absolute path, then the relative path should be relative to your current working directory).

The System.getProperty(String key) method can be used to find the current working directory. The properties are listed in the documentation for the System.getProperties() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to a directory in a Unix environment