Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: File not found

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default File not found

    Hello,

    The following code gives me the result:

    File path is [D:\IMAGES\sample1.pgm]
    java.io.FileNotFoundException: [D:\IMAGES\sample1.pgm] (The filename, directory name, or volume label syntax is incorrect)

    at the line fr = new FileReader

    File file1 = new File(file_path); /*where file_path is a string containing the path, recieved as an argument by the method*/
    		System.out.println("File path is "+ file1.getPath());
     
    		try {
    			fr = new FileReader(file1.getPath());
     
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}

    When I write the path ( fr = new FileReader(D\\IMAGES\\sample1.pgm), it doesn't show that exception anymore, but I want to use the String variable file_path instead of writing it directly (because I want to make it work for ANY file, not that particular file).

    Thank you in advance and have a great day!


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: File not found

    FileReader can take a File as an argument. Try removing the getPath() function when declaring the reader and see what happens.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: File not found

    That output didn't come from the code you posted. In particular there are no square brackets being printed in the line that outputs the file's path.

    Copy, paste and post both the code and the runtime exception exactly as you see them.

    It might be a good idea to also output the value of file_path to check that its value is what you think it is. The fact that the runtime is grumblng about the syntax suggests that possibly you are having trouble with the slashes. Backslashes (\) are a PITA conceptually because they are escaped in String literals but are only a single character in a String instance. It's always easier, in Java, to use forward slashes (/). Also remember that there's a colon ( after a Windows drive letter.

Similar Threads

  1. [SOLVED] jar file not found
    By user2013 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 13th, 2013, 03:12 PM
  2. Replies: 26
    Last Post: February 10th, 2013, 12:59 PM
  3. File not found in class path
    By Jeff.Steinkamp in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 1st, 2013, 08:37 PM
  4. .java File not found
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 17th, 2011, 11:53 PM
  5. File not found exception
    By mwr76 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 10th, 2011, 04:54 PM

Tags for this Thread