• 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

java.io.package

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I know that Constructing an instance of the File class does not create a file on the local file system , instead simply creates an instance of a file or a directory with the specified string, but when will be created the file??
Thank you in advance.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jordi,
To the best of my knowledge you have three different ways of creating the file on disk:
Suppose you have created the File object as follows
File f = new File("file1");
1. create a FileOutputStream with the File object as parameter
and write to it:
FileOutputStream fos = new FileOutputStream(f);
fos.write(...); //write some bytes here
fos.flush();
fos.close();
(NB: this code has to be surrounded by try/catch block in case
of occurence of IOException)
2. Create a FileWriter with the File object as parameter and write to it:
FileWriter fw = new FileWriter(f);
fw.write(...); //write some strings
fw.flush();
fw.close();
(NB: this code has to be surrounded by try/catch block in case
of occurence of IOException and maybe FileNotFoundException)
3. Create a RandomAccessFile object with the File object as parameter and an "access-mode" string ("r" or "rw"), if you create the RandomAccessFile with "r" and the file does not exist on the file system then a FileNotFoundException is thrown. Instead if you create it with "rw", then if the file exists it is opened, otherwise a new one is created.
RandomAccessFile raf = new RandomAccesFile(f,"rw");
raf.writeChars(...); //write some string
raf.close();
(NB: enclose this code with proper try/catch block in case the
file does not exists or some IOExceptions occur)
Hope this helps,
Val
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Val for your help!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic