Results 1 to 5 of 5

Thread: Unhandled Exception Type IOException - Why?

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Unhandled Exception Type IOException - Why?

    With the following code I get an error that prevents compiling on the line that reads:
    Code:
    vecReturns = fileList("C:\\");
    Any idea what's up?

    Code:
    import java.io.*;
    import java.util.*;//For the vectors
    
    public class Test
    {
    	public static Vector fileList(String directoryName) throws IOException
    	{
    		Vector vecReturns = new Vector();
    		File dir = new File(directoryName);
    		if(dir.exists() && dir.isDirectory())
    		{
    			File [] list = dir.listFiles();
    			for (int i = 0;i<list.length;i++)
    			{
    				if(list[i].isFile())
    				{
    					StringBuffer name = new StringBuffer(list[i].toString());
    					StringBuffer result = new StringBuffer(name.length());
    					for(int x = 0;x<name.length();x++)
    					{
    						if(name.charAt(x)=='\\')
    						{
    							result.append("\\\\");
    						}
    						else
    						{
    							result.append(name.charAt(x));
    						}
    					}
    					//vecReturns.add(result.tostring());
    					vecReturns.add(result);
    					System.out.println(result);
    				}
    			}
    			return vecReturns;
    		}
    		else
    		{
    			throw new IOException(directoryName + " is not a directory.");
    		}
    	}//End fileList
    	
    	public static void main(String[] args)
    	{
    		Vector vecReturns = new Vector();
    		vecReturns = fileList("C:\\");
    		
    	}//End main
    	
    }//End Test
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Question

    When I attempt to compile your code, I get

    C:\java\src\t>javac Test.java
    Test.java:21: unclosed character literal
    if(name.charAt(x)=='\')
    ^
    Test.java:28: ')' expected
    }
    ^
    Test.java:29: illegal start of expression
    }
    ^
    3 errors


    Nothing about vecReturns = fileList("C:\\");

    EDITS
    But watch your use of the escape character "backslash" in both the unclosed character literal and your vecReturns.
    Last edited by Phenix; Jan 2nd, 2003 at 02:20 PM.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Looking at your code real quick i dont see where the IOException being thrown is caught. It looks like you need to add a try{}catch(Exception type){} in your main method. You are better off not throwing the Exception and just using a try catch structure in your method.

  4. #4

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Talking

    Thanks!

    I changed my main to look like this:
    Code:
    	public static void main(String[] args)
    	{
    		Vector vecFiles = new Vector();
    		try
    		{
    				vecFiles = fileList("C:\\");
    		}
    		catch (IOException IOerror)
    		{ //  Error
    			System.out.println("Whoops!!");
    		}
    	}//End main
    And it works!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  5. #5

Posting Permissions

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



Click Here to Expand Forum to Full Width