• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileName provided in the FileOutputStream constructor

 
akshitha Akki
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the important points.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic