/**
     * Returns the name of the file or directory denoted by this abstract
     * pathname.  This is just the last name in the pathname's name
     * sequence.  If the pathname's name sequence is empty, then the empty
     * string is returned.
     *
     * @return  The name of the file or directory denoted by this abstract
     *          pathname, or the empty string if this pathname's name sequence
     *          is empty
     */
    public String getName() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) return path.substring(prefixLength);
return path.substring(index + 1);
    }

解决方案 »

  1.   

    File file=new File("21212");
    System.out.println(file.getName());//21212
      

  2.   

    如果是用的eclipse,可以看一下File的源代码,ctrl+File一下!
      

  3.   

    看File 的构造方法
    File(String pathname)
    Creates a new File instance by converting the given pathname string into an abstract pathname.
    这样写似乎没有问题,我怀疑参数名和文件名是一样的文件这块还没有了解坐等大神
      

  4.   

    你告诉它的文件名叫".",你说它返回给你的是什么?那当前路径是这个java 文件所在的路径是吗?为什么呢?
      

  5.   

    File file=new File("."); 创建了一个文件名为“.”的文件
      

  6.   

    file只是内存里的一个对象,不是实际储存在硬盘的文件
      

  7.   

    File f=new File(".");
    默认的就是当前Java文件路径啊,文件名是点。
    如果要在其他地方创建:
    //文件名
    String name = "face.gif";
    //源文件的路径
    File f = new File("d:\\"+name);
    如果路径"d:\\"中含.,那.表示当前路径,不过要注意写法。
      

  8.   

    File f = new File(“D:\\1.txt”);
    f.getName 你这样输出 就是 1.txt!