• 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

I/O question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to read a file in java but I can't due to an error. The code is:

import java.io.*;

class formato
{
public static void main(String args[])
{
try
{
FileReader fis = new FileReader("proveedores.txt");
}
catch (FileNotFoundException ex)
{
System.out.println("File not found");
return;
}
DataInputStream dis;
try
{
dis = new DataInputStream(dis);
}
catch (FileNotFoundException ex)
{
System.out.println("File not found");
}
}
}

When I compile this code I display two errors:

formato.java:22: variable dis might not have been initialized
dis = new DataInputStream(dis);
^
formato.java:24: exception java.io.FileNotFoundException is never thrown in body
of corresponding try statement
catch (FileNotFoundException ex)
^
2 errors

How I can solve them?

Thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, for the first one, you're passing "dis" as an argument to it's own constructor; and of course, it hasn't been initialized yet, no "might" about it. You intended to pass "fis", I imagine.

For the second one, the message is very straightforward. The DataInputStream constructor won't throw a FileNoteFoundException, so it's illegal to try to catch it.
 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I think I have to type the code again because everything is wrong.

Thanks.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might not have to retype it all.

One problem I see is that you define things inside of try like this:



The problem is that with this code the fis Object only exists within the scope of the try statement. You won't be able to use it anywhere else. What you need to do is perhaps something like this:



Either that or you need to put all the code referring to fis inside of the try so it will still be in scope. Basically, anything you declare inside a pair of {} only exists inside them. Once you leave them it is gone. They call that scope.

Hope that helps a bit.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic