Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to a directory in a Unix environment

 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw i originally tried it without the backslashes and it didn't work. I'll try this FileSeparator.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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").
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic