我现在有一个File对象,代表一个目录;一个Stirng对象,代表一个文件名。
但实际上,文件系统中,在这个目录下不存在这个文件的。
我怎么才能在这个目录下用这个文件名创建一个大小为0的文件呢?
File Dir;
......
File file = new File(Dir,"filename.txt");似乎是不行的。

解决方案 »

  1.   

    去看看API文档,有一个createNewFile() 方法
      

  2.   

    File fileTarget = new File("C:/my document/hello","111111111.txt");
             fileTarget.createNewFile();
      

  3.   

    createFile(File path, String name)
    {
      if(!path.exist()){
        path.makdirs();
      }
      FileOutputStream fis = new FileOutputStream(path.getAbs??Path() + "/" + name);
      fis.close();
    }
      

  4.   

    File Dir = new File("c:\\");
    File f = new File(Dir,"test.txt");
    //如果不存在,创建新文件。
    //如果存在,什么都不做。
    try {
    f.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }