0

I am writing a code that asks if a file path is null. If the path is null I need to return false. The problem that I am having is that the method is not returning false. I can't tell if the method is checking if the filePath exists or not. Here is my code:

public boolean saveWordTable(List<Word> wordTable, String filePath) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    if (wordTable == null) {
        return false;
    }
    if (!(new File(filePath).exists())) {
        return false;
    }


    try {
        oos.writeObject(wordTable);
    } catch (IOException e) {
        //e.printStackTrace();
        return false;
    }

    fos.close();
    oos.close();
    return true;

Any kind of help would be appreciated.

  • 1
    You the Files.notExists(path) method. https://docs.oracle.com/javase/tutorial/essential/io/check.html – Incognito Apr 08 '17 at 19:04
  • 1
    Does your code behave differently from the desired? If so, please specify the difference precisely. “Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve).” (from [What topics can I ask about here?](http://stackoverflow.com/help/on-topic)) – Ole V.V. Apr 08 '17 at 20:04
  • 1
    see: http://stackoverflow.com/questions/468789/is-there-a-way-in-java-to-determine-if-a-path-is-valid-without-attempting-to-cre/35452697#35452697 – c0der Apr 09 '17 at 14:40

0 Answers0