How do I check if a file exists in Java?

后端 未结 17 2417
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:50

How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl\'s -e $filename

17条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 01:39

    first hit for "java file exists" on google:

    import java.io.*;
    
    public class FileTest {
        public static void main(String args[]) {
            File f = new File(args[0]);
            System.out.println(f + (f.exists()? " is found " : " is missing "));
        }
    }
    

提交回复
热议问题