It is very easy to check if file exists or not in your file system. You can use java.io.File’s exists() method to check if file exists or not.
Java program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package org.arpit.java2blog; import java.io.File; public class FileExistsMain { /* * @author Arpit Mandliya */ public static void main(String[] args) { System.out.println("-----------------"); // Read the file File mp3File=new File("/Users/Arpit/Desktop/MySong.mp3"); if(mp3File.exists()) { System.out.println("MySong.mp3 file exists"); } else { System.out.println("MySong.mp3 file does not exist"); } System.out.println("-----------------"); File mp3FileFolder=new File("/Users/Arpit/Desktop/MySong"); if(mp3FileFolder.exists()) { System.out.println("MySong folder exists"); } else { System.out.println("MySong folder does not exist"); } System.out.println("-----------------"); } } |
1 2 3 4 5 6 7 |
----------------- MySong.mp3 file exists ----------------- MySong folder does not exist ----------------- |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.