|
|
|
|
![]() ![]() |
Mar 10 2008, 03:24 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 245 Joined: 11-June 05 From: india-mumbai Member No.: 8,095 |
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 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(); } } |
|
|
|
Mar 10 2008, 05:25 PM
Post
#2
|
|
|
Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 975 Joined: 25-September 05 From: The dungeon deep below the foundation of trap17 Member No.: 12,251 |
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();
|
|
|
|
Mar 12 2008, 04:37 AM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 225 Joined: 8-April 05 Member No.: 5,385 |
QUOTE 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): 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; 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! |
|
|
|
Mar 12 2008, 05:55 AM
Post
#4
|
|
|
Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 975 Joined: 25-September 05 From: The dungeon deep below the foundation of trap17 Member No.: 12,251 |
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.
|
|
|
|
Mar 13 2008, 12:17 PM
Post
#5
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 245 Joined: 11-June 05 From: india-mumbai Member No.: 8,095 |
thanks it worked :-)
|
|
|
|
Apr 20 2008, 02:30 AM
Post
#6
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 25 Joined: 19-April 08 Member No.: 61,043 |
wouldnt it have been to use the scanner class to read a file object line by line? instead of creating buffers and the such?
|
|
|
|
Jun 1 2008, 09:14 PM
Post
#7
|
|
|
Newbie ![]() Group: Members Posts: 6 Joined: 9-February 08 From: Raipur Member No.: 57,603 |
just try it by initializing the "fin" with null.as like fin=null
|
|
|
|
Jun 1 2008, 09:26 PM
Post
#8
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 280 Joined: 5-October 07 From: Random Places Member No.: 51,171 ![]() |
Well, fin was a file input stream, which cannot be given a value of null. I myself and just learning Java.
|
|
|
|