如何用Flie类的createNewFile方法,在指定路径下创建一个文件?

解决方案 »

  1.   

    File f = new File("C:/temp/test.txt");
    f.createNewFile();
      

  2.   


      File file=new File("1.txt"); //可以指定路径名
      if(!file.exists()) 
      file.createNewFile(); 
      

  3.   

    File f = new File("F:\\1111.txt");
    f.createNewFile();
      

  4.   


    import java.io.File;
    import java.io.IOException;
    public class TestFile {
    public static void main(String[] args){
    java.io.File file = new File("H:/dd.txt");
    try {
    file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  5.   

    File file = new File("指定路径");
    file.createNewFile();
      

  6.   

    查看API文档
    如下:
    createNewFile
    public boolean createNewFile()
                          throws IOException当且仅当不存在具有此抽象路径名指定的名称的文件时,原子地创建由此抽象路径名指定的一个新的空文件。检查文件是否存在,如果不存在则创建该文件,这是单个操作,对于其他所有可能影响该文件的文件系统活动来说,该操作是原子的。 
    注:此方法不应 用于文件锁定,因为所得到的协议可能无法可靠地工作。应该使用 FileLock 机制替代。