How to determine if a file exists in Java

1 Answer

0 votes
import java.io.File;
import java.io.IOException;
 
public class MyClass {
 
    public static void main(String[] args) throws IOException {
 
        try {
 
            File file = new File("d://data.txt");
             
            boolean fileExists = file.exists();
 
            if (fileExists) {
                System.out.println("file exists");
            } else {
                System.out.println("file not exists");
            }
 
        } catch (Exception e) {
            System.out.print(e);
        }
    }
}
 


 
/*
         
run:
   
file exists
     
 */
 
 


 

 



answered Nov 17, 2016 by avibootz
edited Oct 9, 2023 by avibootz

Related questions

1 answer 181 views
1 answer 175 views
2 answers 217 views
1 answer 187 views
1 answer 111 views
111 views asked Oct 9, 2023 by avibootz
3 answers 169 views
1 answer 136 views
...