• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileName provided in the FileOutputStream constructor

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the important points.
reply
    Bookmark Topic Watch Topic
  • New Topic