• 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

FileReader question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there in BB and KS book page 439

"2. Create a Reader or a Writer or a Stream. Specifically, create a FileReader, a
FileWriter, a PrintWriter, a FileInputStream, or a FileOutputStream.
Whenever you create an instance of one of these classes, you automatically
create a file, unless one already exists,"

I tried to do this:


File f2 = new File("File3");

System.out.println("does the file2 exist?: " + f2.exists());

FileReader fr2 = new FileReader(f2);


When I run it:

does the file2 exist?: false
Exception in thread "main" java.io.FileNotFoundException: File3 (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at chapter7.files.FileTest2.main(FileTest2.java:54)

Can someone pls explain me why the file didn't create when I created the FileReader instance?.. thanks.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey hi,

I tried the code that you have posted. Even I am getting the same exception.
But when you replace FileReader with FileWriter, it works.

Also if you will see the API on page 436 for FileReader...you'll notice that there is only read() method......so I think there is no sense in creating a new File(when file is not present) and reading it...since new file would be BLANK....so read() would return NULL.....
[ May 13, 2008: Message edited by: Vinayak Jallapelli ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File objects can be created for anything and everything, whether it exists or not (A File object can be used for creating a file, though, and for checking whether a file exists, so it makes sense to create them for non-existing files.)

You can't read from non-existing files, so the exception shouldn't surprise. Note that non-existing files are not the same as zero-sized files, so FileReader shouldn't go ahead and create a file if it doesn't exist.

On the other hand, it is perfectly possible to write to non-existing files; that's why you're not getting an exception there.
[ May 13, 2008: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think only for writer objects or outputstreams only new file will be created if it is not existing,i got same exception.
[ May 13, 2008: Message edited by: ramesh maredu ]
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

File f = new File("file1.txt") does not create the file on the disk, it just checks whether the file is there and will not create if the file if it is not there, thta is why when we check f.exists(), it gives false..
To create the file on disk if file doesn't exits use f.createNewFile()
and now check f.exists();


I think you must first write th file and read

File f = new File("file1.txt")
FileWriter fw = new FileWriter() // this wil create the new file and and also FileWriter object

fw.write("hello");

FileReader fr = new FileReader(f)
String s = f.read() // this will read the whole file


Please correct me if i am wrong.
[ May 13, 2008: Message edited by: Dinesh Tahiliani ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic