File f1= new File("D:\\3000.txt");
File f2= new File("D:\\2000");
System.out.println(f1.exists());
System.out.println(f2.exists());结果是false,false
D盘里面也没有任何新文件和新目录出现,请问这到底是怎么回事?

解决方案 »

  1.   


    File file = new File("D:/test.txt");
    file.createNewFile();
      

  2.   

    你new一个File的时候,是不会创建文件的吧...
      

  3.   

    File 只是包含一个文件路径的引用,并不是物理意义上的文件。
      

  4.   

    if(!f1.exists())
    {
    f1.createNewFile();
    )
      

  5.   

    File file = new File("D:/test.txt");
    这句代码起到什么作用?
    还有为什么不是D:\\呢?
      

  6.   

    File file = new File("D:/test.txt"); 或者:File file = new File("D:\\test.txt"); 都是可以的,只不过是路径的表示风格不同而已。
      

  7.   

    linux或unix的目录之间的分隔符是/,而且/在字符串中不需要转义,所以好多人习惯于用D:/test.txt这种格式的。
      

  8.   


    import java.io.File;
    public class Test {
    public static void main(String[] args) throws Exception{
    File f1= new File("D:\\3000.txt"); 
       File f2= new File("D:\\2000"); 
       f1.createNewFile();          //这里才是真正创建了一个文件
       f2.createNewFile();
    System.out.println(f1.exists()); 
    System.out.println(f2.exists());
    }
    }
      

  9.   

    File f1= new File("D:\\3000.txt"); 只是声明了一个文件对象,并没有对文件进行任何操作。
    如果你读取,或者写入,才会真正的进行磁盘或者网络的IO操作。我们看看File的源代码
      public File(String pathname) {
        if (pathname == null) {
          throw new NullPointerException();
        }
        this.path = fs.normalize(pathname);
        this.prefixLength = fs.prefixLength(this.path);
      }看到了,他只是做了很简单的文件名的处理而已。
      

  10.   

    另附:为了安全起见一般用“/”(或者用separator);因为\\这个只能在windows下可行,linux下就不行。