Results 1 to 5 of 5
  1. #1
    Iman is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Problem in reading from file. Please help.

    Hi, Help is really appreciated :

    I am trying to read a file and encrypt it as a part of my work.
    The problem is:

    1- If I use FileInputStream fstream = new FileInputStream(filename.toString()); It gives me an error java.io.FileNotFoundException:
    Note that if the sentence
    System.out.println(" incoming file name="+filename); prints incoming file name= C:\mydir\Interface.java

    2- If I use FileInputStream fstream = new FileInputStream("C:\\mydir\\Interface.java"); It works

    I need to use the first method as my application obtains the file name with the full path from a JTree. It tooks me too much time and can not figure why.

    Thanks a lot

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

    Default Re: Problem in reading from file. Please help.

    A character preceded by a backslash is an escape sequence - meaning the sequence has special meaning. If you wish to have a backslash literal, you must escape the backslash, as your #2 illustrates.

    See: Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

  3. #3
    Iman is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem in reading from file. Please help. java.io.FileNotFoundException

    Quote Originally Posted by doWhile View Post
    A character preceded by a backslash is an escape sequence - meaning the sequence has special meaning. If you wish to have a backslash literal, you must escape the backslash, as your #2 illustrates.

    See: Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
    I tried to use string split and re-construct the string by adding "//" in between, but did not work as well.
    String str = new String();
    String [] flist = filename.toString().split("\\\\");
    for (int i=0; i<flist.length; i++)
    if (i<flist.length-1)
    str=str+ flist[i]+"\\"+;
    else
    str=str+ flist[i];
    I still got the same problem
    java.io.FileNotFoundException:

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,539
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem in reading from file. Please help.

    Either the file really doesn't exist (not very likely) or the method filename.toString() produces an incorrect file name; print out the string character by character and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Problem in reading from file. Please help.

    What class is 'filename'?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. reading a file problem
    By aianta in forum New To Java
    Replies: 3
    Last Post: 08-03-2011, 07:22 PM
  2. Reading from file problem
    By BillyB in forum New To Java
    Replies: 10
    Last Post: 03-18-2011, 04:09 AM
  3. problem reading file
    By jmoutia in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 03:19 AM
  4. Problem reading from a file
    By sarapeace in forum New To Java
    Replies: 13
    Last Post: 10-03-2010, 02:08 PM
  5. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 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
  •