error: "unreachable statement in File.close();"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • natie
    New Member
    • Oct 2010
    • 5

    #1

    error: "unreachable statement in File.close();"

    I have the following code which is a method that displays the content of a file with the name "File"

    Code:
     public void DisplayAll()throws IOException
    
        {
           		try
    			{DataInputStream F = new DataInputStream(new FileInputStream("File"));
    			{
        			for(;;)
        			{
        			System.out.println(F.readUTF());
        			System.out.println(F.readInt());
        			System.out.println(F.readInt());
        			}
    
          		}
    
    			F.close();
    			}catch(EOFException eof){}
    
       	}
    the error is: "unreachabl e statement" at the "F.close". what I'm supposed to do?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    in line 7
    Code:
        for(;;)
           ...
    you have an infinite loop, i.e. there is no exit condition so the F.close() in line 16 can never be reached.
    there should be an exit condition for the loop - I would suggest putting the F.close() in the catch EOFException(), e.g.
    Code:
            DataInputStream F=null;
            try
                {
                 F = new DataInputStream(new FileInputStream("File"));
                 for(;;)
                    {
                    System.out.println(F.readUTF());
                    System.out.println(F.readInt());
                    System.out.println(F.readInt());
    
                }       
             }          
             catch(EOFException eof) { F.close();}
             catch(IOException e) { System.out.println(e) ;}

    Comment

    Working...