4

I wonder if anyone knows why I may be getting a java.io.FileNotFoundException when I'm trying to find a file that I know exists in the directory.

I think the following have something to do with it, please let me know if I'm correct or if there's something else:

  1. I downgraded my JVM from 1.7 to 1.6
  2. The file name contains two question marks, so the file is called filename_?)?.data

While I was using JVM 1.7, the program was able to find the file and open it. However, after downgrading to 1.6, it looks like it can't find this particular file. So I'm thinking maybe JVM 1.6 can't read files w/ question marks in them.

Also, I double/triple checked and the file does exist in the directory my program is looking in (its able to find other files in there as well).

Here's my code below:

public Object readFromFile(String fileName) {
    // Check for null
    if (fileName == null || fileName.equals("")) return null;

    Object obj = null;
    ObjectInputStream input = null;

    // Open file into (input)
    try {
        input = new ObjectInputStream(new FileInputStream(fileName + ".data"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Read content of file into (obj)
    try {
        obj = input.readObject();
        input.close();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return obj;
}
Guido
  • 926
  • 2
  • 10
  • 19
FilmiHero
  • 2,306
  • 7
  • 31
  • 46
  • 3
    What API are you using to "find the file"? Without some code we can only guess at what's wrong. Please read the [FAQ] and [Ask] for tips on writing good SO questions. – Jim Garrison Jan 29 '13 at 20:58
  • Sorry. Forgot to add the code. I've updated my question. – FilmiHero Jan 29 '13 at 21:01
  • Are you sure about the file path being contained by the variable `fileName`? Try to display the value of this variable and see what it contains and see if it refers to the actual file location. – Lion Jan 29 '13 at 21:07
  • I know that the function and `fileName` work because my program pulls other files from the directory with various other `fileNames`. – FilmiHero Jan 29 '13 at 21:13
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 29 '13 at 21:14
  • Thanks @AndrewThompson, I'll check out the link and keep it in mind next time around. – FilmiHero Jan 29 '13 at 21:25

1 Answers1

2

probably you need to encode your filename when it is using special chars

Try this

String fileNameNew= java.net.URLEncoder.encode(fileName);
if (fileNameNew == null || fileNameNew.equals("")) return null;

Object obj = null;
ObjectInputStream input = null;
...

and you might check here: How to determine if a String contains invalid encoded characters

Community
  • 1
  • 1
Guido
  • 926
  • 2
  • 10
  • 19
  • Thanks. Looks like that did the trip. I just added the `URLEncoder.encode` to my `readFromFile` and `writeToFile` function and it works. – FilmiHero Jan 29 '13 at 21:21
  • 1
    I can't imagine why this should work, although I see that the OP thinks it does. File systems don't use URL form encoding. – user207421 Jan 30 '13 at 03:24