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 170 views
1 answer 164 views
2 answers 207 views
1 answer 176 views
1 answer 98 views
3 answers 150 views
1 answer 127 views
...