File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes unable to locate file from FileOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "unable to locate file from FileOutputStream" Watch "unable to locate file from FileOutputStream" New topic
Author

unable to locate file from FileOutputStream

Brian Carlisle
Greenhorn

Joined: Oct 30, 2012
Posts: 19
Hello

I am studying for the SCJP certification and currently working through the Heads First Java topic on Serialization. After executing a very basic test app that writes to a FileOutputStream I do not see the physical file on my C drive.

Code below compiles and runs but no file. I saw some examples without the full path but that confused me even more. where would the default location be if you do not specify it? Thanks!


Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Brian Carlisle wrote:
try {
FileOutputStream fs = new FileOutputStream("C:\\MYBOX.txt");
ObjectOutputStream obj = new ObjectOutputStream(fs);
obj.writeObject(myBox);
obj.close();
} catch (Exception e1) {
e1.printStackTrace();
}

Well, certainly try obj.flush() before obj.close().

The String literal for the filename and path seem correct, although that file will not be a text file of characters even if you name it .txt.

Since there is no exception then it would make sense to find it in the root, but without a flush() it's possible to get no exception and no file either.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Paul Witten wrote: . . . Well, certainly try obj.flush() before obj.close(). . . .
Not convinced that will make any difference, I am afraid. Most close() methods flush the stream too, but it doesn’t say anything about that in the ObjectOutputStream documentation.
The problem is about the existence of the file. Try looking in the root directory of your C drive (by no means a good place to put a single file). Try looking for a new C directory in your current directory. Try creating an empty file in your required location. Try using new File("myfile"). Try the relative path; if you write new File("myfile") without folder names, it will appear in the directory you happen to be in (current directory).
I tried your code with minor changes on a Linux box and got a load of exceptions about no such file, so there are obviously system differences in its behaviour.

I am not convinced obj is a good name for a Stream. Try a different name for the Stream.

And welcome to the Ranch I have edited your code with code tags, which you should always use, and you can see how much better it looks. You should not ues tabs for indentation; use spaces.
Brian Carlisle
Greenhorn

Joined: Oct 30, 2012
Posts: 19
Thanks for the replies and the assistance. Code tags look much better ;)

Apparently there is some level of protection against writing direct to the root directory (i guess in the JVM or OS?). If i specify a subdirectory with double \\ it seems to work just fine as in..



flush() had no effect in my case but i appreciate the help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

Yes, I believe that recent versions of Windows prevent you from creating files in the root directory. That was probably what was happening. Although you should have been seeing IOExceptions thrown... perhaps you just overlooked the stack trace, or wrote it to a place where you weren't looking?
Brian Carlisle
Greenhorn

Joined: Oct 30, 2012
Posts: 19
Yes you are correct. I have been using the command line and eclipse to run. the eclipse console was not showing the exceptions.

Shame on me...
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Brian Carlisle wrote:Yes you are correct. I have been using the command line and eclipse to run. the eclipse console was not showing the exceptions.

Shame on me...

What do you mean, "Shame on me"? You mean you saw a stack trace? And it didn't show in Eclipse? That's bizarre.

Speaking of forbidden paths, my Vista box has several paths that even an Admin can't read, let alone write to. What a strange system. Either you are an admin or you're not, right? Not in GatesWorld. At least on Vista you have Admin and SlightlyLessThanAdminButBigEnoughToMakeYouFeelPowerful.


Brian Carlisle
Greenhorn

Joined: Oct 30, 2012
Posts: 19
Shame on me for not using the command line

After reading the advice from the thread i re-ran the program using the command line and saw the exception. When i run thru eclipse i do not see anything in the console. it runs and terminates looking like it completed. Maybe my log4j settings or runtime arguments are not right. Not sure.

I am running Windows 7 Professional edition. would love to get Linux up and running but one thing at a time. I work at a Unix shop as a Cobol programmer (LOL) for 13 years. Been exposed to java for about 5 but using proprietary framework written by Ben Galbraith (rockstar). Pretty cool stuff but not what you need to know for certification.

Eclipse is a great tool but provides too much help i think for someone wanting to have a command of the syntax. I was using primarily because i can interactively link to java class files for studying purposes.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I am surprised you are not seeing the stack trace on Eclipse; it usually appears on the console subwindow (in red because it uses System.err not System.out). It doesn’t vanish when I use Eclipse.

Desperate suggestion to preserve the stack trace. Add the following after the last line of your main method and before its closing }
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Brian Carlisle wrote: I work at a Unix shop as a Cobol programmer (LOL) for 13 years.

Brian, how do you feel about working on legacy systems? Is it as confining as I sometimes imagine it to be (without having done it)?

Since you seem to be branching out to Java it would appear as though you see something there that you're not getting in your regular job. What is it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
There are millions of lines of COBOL code still is use, going back to the 1960s, and many companies still depend on that code. There are very few people who can maintain it, so knowing COBOL is probably still a key to a big salary
Brian Carlisle
Greenhorn

Joined: Oct 30, 2012
Posts: 19
It depends on where you work i think. On mainframe environments it can be very mundane. I have worked at the same company for pretty much my entire career. started out a HPUX shop running Wang Cobol and ISAM files. Now we use the same Cobol code and migrated to an Oracle DB. Then we brought up a GUI front end but still used the same base code. Pretty great to be exposed to so much over the years.

Had the opportunity to go into management but i love to code and Java seemed like the right fit. It really is a mind shift need to look at things differently than what i was used to. But it is great to learn new things. Looking forward to getting certified very dificult with a full workload and personal responsibilities.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: unable to locate file from FileOutputStream