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

FileName provided in the FileOutputStream constructor

akshitha Akki
Greenhorn

Joined: Aug 03, 2012
Posts: 24
This is a simple program to get the control flow on the bytes getting added/written to a file.

Doubt:
Here the filename provided is without the specified path and the program gets executed normally.
So,
1.FileOutputStream op = new FileOutputStream("testPgm.txt");
-- this line creates a file if that does not exists already?
-- since path is not provided where will the file gets stored by default?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

akshitha Akki wrote: -- this line creates a file if that does not exists already?

Yes.

akshitha Akki wrote: -- since path is not provided where will the file gets stored by default?

In the current directory (the directory that is the current directory) when you run the program.

CodeRanch forum tip: Please UseCodeTags when you post source code, so that your code is formatted nicely as you can see above. (I added the code tags for you above).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
Also if you have JDK 7 you can use try with resources statement, this new form of try automatically closes any object that implements AutoCloseable interface. As you know, after reading or writing to a file you have to close it using close() method, but if you use this new try statement, this will be done automatically for you.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Okti Wmcijewski wrote:Also if you have JDK 7 you can use try with resources statement, this new form of try automatically closes any object that implements AutoCloseable interface. As you know, after reading or writing to a file you have to close it using close() method, but if you use this new try statement, this will be done automatically for you.

And if you don't have Java 7 you should put your close in a finally block. that way the file will still be closed even if your write call throws an exception.
akshitha Akki
Greenhorn

Joined: Aug 03, 2012
Posts: 24
Thanks all for the important points.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileName provided in the FileOutputStream constructor