Honesty Rocks! truth rules.

Where Am I Making Mistake

HOME      >>       Programming

apurva

hello i have just started to learn java by myself and i am stuck here with this example
i want to show a file in console please tell me where am i making mistake.

i am getting erroe variable fin maight not be initialised

here is the code

/** Display a text file To use this program,specify the name of file that you want to see */ import java.io.*; class showfile { public static void main(String args[])throws IOException { int i; FileInputStream fin; try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter file name:"); String filename=br.readLine(); fin=new FileInputStream(filename); } catch(FileNotFoundException e) { System.out.println("File not found"); return; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Ussag:Showfile file"); } //Read characters untill Eof id encountered do { i=fin.read(); if(i!=-1) System.out.println((char)i); } while(i!=-1); fin.close(); } }


galexcd

You are getting that error because it is telling you that fin may not be set since your doing that in the try&catch loop, and then you are doing methods of the FileInputStream class with a variable that possibly may be empty. Put your do-while loop and fin.close(); inside the try, or you could try setting fin to an empty file if you want in the two catches, but I'm not sure if that would work or not. Your best bet is to move the do-while loop and your fin.close();


CrashCore

try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name:");
String filename=br.readLine();
fin=new FileInputStream(filename);

}
catch(FileNotFoundException e)
{
System.out.println("File not found");
return;

}


You have a couple problems yet. First, you should not declare the file input stream in the same try/catch as the BufferedReader on standard input. Separate these. Also, you are continuously redefining the InputStream in the later part of the code, which will make the program simply run an infinite loop. A corrected version of the code would look something like this (depending upon what you wanted it exactly to do):

/** Display a text fileTo use this program,specify the name of file that you want to see*/import java.io.*;class showfile{ public static void main(String args[])throws IOException { int i; FileInputStream fin; String filename = ""; try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter file name:"); filename=br.readLine(); } catch(FileNotFoundException e) { System.out.println("File not found"); return; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Ussag:Showfile file"); } //Open the FileInputStream try{ fin=new FileInputStream(filename); } catch(FileNotFoundException e){ e.printStackTrace(); System.out.println("File not found"); return; } //Read characters untill Eof id encountered do { i=fin.read(); if(i!=-1) System.out.println((char)i); } while(i!=-1); fin.close(); }}

If you have any questions, just let me know. I hope this helps!

Notice from rvalkass:

Code tags added around code.


galexcd

Even though its not good programing practice, defining BufferedReader in the try was not causing any error because it was not being referenced at all outside of the block. And as for the infinite loop, that wont happen unless the file is infinitely long which is impossible.


apurva

thanks it worked :-)


roooss

wouldnt it have been to use the scanner class to read a file object line by line? instead of creating buffers and the such?


vivek mahajan

just try it by initializing the "fin" with null.as like fin=null


coolcat50

Well, fin was a file input stream, which cannot be given a value of null. I myself and just learning Java.


galexcd

just try it by initializing the "fin" with null.as like fin=null

Initializing fin to null is no different than initializing it and not setting any value for it. Besides this topic has been resolved.

Well, fin was a file input stream, which cannot be given a value of null. I myself and just learning Java.

Well, fin is really just a class, and any class can be null, it just means it is an empty reference to a class with no data in it.

pandu

You know, it's much simpler to use a Scanner. Here:import java.util.Scanner;Scanner s=new Scanner(new File("filenamehere.txt"));String temp;while(s.hasNext()){temp=s.next(); //gives you the next token(word)System.out.println(temp);}


shotgun

I think that the variable that was not initiated was i(maybe others too but specially this one), but as it was written before the easiest and practical way to get an input is using java.util.Scanner. As you may know java.util.* brings many helpful classes to avoid the programmer waste time in simple things.[tab][/tab]? As you said before that you where learning java, it would be a good thing if you review some of the java code of the classes that are inside java.util. Greetings.


snowisawsome

I recommend an extremely simple solution that may or may not work. Try this:

 

= null;

String filename = "";

 

_linenums:0'>/** Display a text fileTo use this program,specify the name of file that you want to see*/import java.io.*;class showfile{ public static void main(String args[])throws IOException { int i; FileInputStream fin <strong class='bbc'>= null</strong>; String filename = "";


galexcd

I recommend an extremely simple solution that may or may not work. Try this:

Please read all of the replies before posting. Here is my reply to someone who suggested the exact same fix:

Initializing fin to null is no different than initializing it and not setting any value for it. Besides this topic has been resolved.