那你可以先判断
是否存在 boolean exists()
          Tests whether the file denoted by this abstract pathname exists.

解决方案 »

  1.   

    先根据你的文件路径定义一个文件对象
    File Myfile = new File("d:\myfile.txt");
    if( Myfile.exists()){
    //文件存在  
    }
    else{
    //文件不存在}
      

  2.   

    if(文件不存在时)
      创建文件;如果在这样两个函数调用之间别的进程创建了这个文件,就会被它删除。
    希望这两步操作能是原子的,不会被打断。在Windows 下的CreateFile(...)就有个标志,叫CREATE_NEW,可以规定在要创建的文件已
    存在时不要删除它。java里不知道有没有相应的方法。
      

  3.   

    Java中对文件的操作   java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。   1。新建目录<%@ page contentType="text/html;charset=gb2312"%>
    <%
    String filePath="c:/aaa/";
    filePath=filePath.toString();//中文转换
    java.io.File myFilePath=new java.io.File(filePath);
    if(!myFilePath.exists())
    myFilePath.mkdir();
    %>  2。新建文件<%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.io.*" %>
    <%
    String filePath="c:/哈哈.txt";
    filePath=filePath.toString();
    File myFilePath=new File(filePath);
    if(!myFilePath.exists())
    myFilePath.createNewFile();
    FileWriter resultFile=new FileWriter(myFilePath);
    PrintWriter myFile=new PrintWriter(resultFile);
    String strContent = "中文测试".toString();
    myFile.println(strContent);
    resultFile.close();
    %>