apurva 0 Report post Posted March 10, 2008 hello i have just started to learn java by myself and i am stuck here with this examplei 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(); } } Share this post Link to post Share on other sites
galexcd 0 Report post Posted March 10, 2008 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(); Share this post Link to post Share on other sites
CrashCore 0 Report post Posted March 12, 2008 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. Share this post Link to post Share on other sites
galexcd 0 Report post Posted March 12, 2008 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. Share this post Link to post Share on other sites
apurva 0 Report post Posted March 13, 2008 thanks it worked :-) Share this post Link to post Share on other sites
roooss 0 Report post Posted April 20, 2008 wouldnt it have been to use the scanner class to read a file object line by line? instead of creating buffers and the such? Share this post Link to post Share on other sites
vivek mahajan 0 Report post Posted June 1, 2008 just try it by initializing the "fin" with null.as like fin=null Share this post Link to post Share on other sites
coolcat50 0 Report post Posted June 1, 2008 Well, fin was a file input stream, which cannot be given a value of null. I myself and just learning Java. Share this post Link to post Share on other sites
galexcd 0 Report post Posted June 16, 2008 just try it by initializing the "fin" with null.as like fin=nullInitializing 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. Share this post Link to post Share on other sites
pandu 0 Report post Posted July 3, 2008 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);} Share this post Link to post Share on other sites
shotgun 0 Report post Posted August 22, 2008 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. Share this post Link to post Share on other sites
snowisawsome 0 Report post Posted November 23, 2008 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 = ""; Share this post Link to post Share on other sites
galexcd 0 Report post Posted November 23, 2008 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. Share this post Link to post Share on other sites