How do you know that certain files exist in Java?

Asked 2 years ago, Updated 2 years ago, 152 views

How do I know if there is a file before I read it in Java? In Pearl, you know when you say -e $filename.

I looked it up and asked how to check if there is a file when writing a file, and I told you to use FileWriter

I think what I want is a little different when I read it

Readable

Is there a way to make an exception if there is no file open and return the existence of the file as true/false?

java file-io io

2022-09-22 22:34

1 Answers

Write java.io.File.

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { 
    // // do something
}


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.