Results 1 to 3 of 3
  1. #1
    aabdi406 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Exception in Thread..

    Hi all,

    I was practising java filestream and I created a little program and when I run it is giving me an error that I am finding difficult to resolve, can anyone spot why It isn't accepting a variable that I declared "myFile" below is the error followed by the code:

    Error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    myFile cannot be resolved
    myFile cannot be resolved

    at FileInputStreamDemo.main(FileInputStreamDemo.java: 37)


    The Program:

    Java Code:
    mport java.io.*;
    
    import java.nio.*;
    
    public class FileInputStreamDemo {
    
    	public FileInputStreamDemo(String string) {
    		// TODO Auto-generated constructor stub
    	}
    
    	/**
    	 * @param args
    	 */
       public static void main(String[] args) {
    		// TODO Auto-generated method stub
    	try  {	
    			FileInputStream myFile = null;
    			
    			myFile = new FileInputStream("abc.dat");
    			
    			boolean eof = false;
    			
    			while(!eof)
    			{
    				int byteValue = myFile.read();
    				System.out.print(byteValue + " ");
    				if (byteValue == -1)
    					eof = true;
    				
    				
    			}
    			
    	   } catch (IOException e){
    	   
    	     System.out.println("Couldn't read file: " + e.toString());
    	   }finally {
    		 if(myFile != null)
    			 try {
    				 myFile.close();
    				 
    			 }catch (Exception e1){
    				 e1.printStackTrace();
    			 }
    				 
    	   }
    	   
    	}
    	   
    	
    
    	}// end class

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    import java.nio.*;

    this isn't out yet or is it?


    java.nio package is new from Java 7, but the current latest version is Java 6 build 24. You can still though get Java 7 for download.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    this isn't out yet or is it?
    Been available for quite some time (since 1.4 : java.nio (Java Platform SE 6) )

    To the OP, check the scope if the variable giving you problems. It is defined within the try, but then the code attempts to access the variable outside that scope (in other words, declare it outside the try)

Similar Threads

  1. Replies: 6
    Last Post: 03-11-2011, 03:53 AM
  2. Replies: 3
    Last Post: 02-03-2011, 04:49 PM
  3. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  4. Exception in thread main
    By maya700 in forum New To Java
    Replies: 14
    Last Post: 06-27-2010, 07:59 PM
  5. Replies: 3
    Last Post: 11-06-2008, 04:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •