import java.io.*;
import java.util.*;class FileExist 
{
public static void main(String[] args) 
{
File dir = new File("d:/1.txt");
if(dir.exists()) System.out.println("The file exist");
else System.out.println("The file is not exist");
}
}

解决方案 »

  1.   

    public boolean fileExists(String filename)
    {
      return new File(filename).exists();
    }
      

  2.   

    File f=new File("c:/data.txt");
    if(f.exists()){
       ....//存在
    } else {
       ....//not exists
    }
      

  3.   

    如果没有文件的话,在操作的时候会抛出一个FileNotFoundException的
      

  4.   

    import java.io.*;
    import java.util.*;class test 
    {
    public static void main(String[] args) 
    {
    File dir = new File("d:/1.txt");
    if(dir.exists()) System.out.println("The file exist");
    else System.out.println("The file is not exist");
    }
    }
      

  5.   

    FileInputStream fin=new FileInputStream("d:/1.txt") 
    会抛出FileNotFoundException的异常.
      

  6.   

    呵呵,new File(aFileName).exists()比较好
    不过最好指定下名字,随便产生一个new可不是一个好习惯