File(File parent, String child) 
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
  其中“抽象路径”具体指的是什么,最好举个例子

解决方案 »

  1.   

    file.getAbsoluteFile() 
    打印出来自己看看
      

  2.   

    我错了, 无视上面吧
    应该就是new parent的时候所用的路径吧
      

  3.   

    根据JDK文档描述:
    java.io.File.File(File parent, String child)
    Creates a new File instance from a parent abstract pathname and a child pathname string. 
    那个abstract pathname指的是构造parent时候使用的路径。注意java对文件路径的分类:1. path,普通路径名,由getPath()方法返回,就是你构造File时指定路径名。如File f = new File("testFile.txt");则f.getPath()返回的就是"testFile.txt".
    2.abstract pathname,根据File的toString方法的说明,这个也是getPath()返回的结果,和上面的一样。
    String java.io.File.toString()
    Returns the pathname string of this abstract pathname. This is just the string returned by the getPath method. 3. absolute path,未修饰的绝对路径,像表示上一级路径的符号"..",在absolute path中是直接显示的。
    如File abstractf = new File("..\\testFile.txt");
    System.out.println(abstractf.getAbsolutePath());
    结果就是 D:\springWorkSpace\Fortest\..\testFile.txt4. canonical path:修饰过的绝对路径。上面abstractf路径中的".."会被解释成上一级路径。如:
    System.out.println(abstractf.getAbsolutePath());
    结果就是 D:\springWorkSpace\testFile.txt
    最简单的方法就是打印出file的getPath()或toString()方法的结果,这个就是abstract path。
    例子: public static void main(String[] args){
    try {
    File f = new File("testFile.txt");
    File abstractf = new File("..\\testFile.txt");
    File emptyf = new File("");
    System.out.println("f's abstract path:" + f.getPath());
    System.out.println("f.getAbsolutePath():" + f.getAbsolutePath());
    System.out.println("f.getCanonicialPath():" + f.getCanonicalPath());
    System.out.println("abstractf's abstract path:" + abstractf.getPath());
    System.out.println("abstractf.getAbsolutePath():" + abstractf.getAbsolutePath());
    System.out.println("abstractf.getCanonicialPath():" + abstractf.getCanonicalPath());
    System.out.println("emptyf's abstract path:" + emptyf.getPath());
    System.out.println("emptyf.getAbsolutePath():" + emptyf.getAbsolutePath());
    System.out.println("emptyf.getCanonicialPath():" + emptyf.getCanonicalPath());
    File newFile = new File(emptyf,"newFile.txt");
    System.out.println("-------------");
    System.out.println("newFile's abstract path: " + newFile.getPath());
    System.out.println("newFile's absolute path: " + newFile.getAbsolutePath());
    System.out.println("newFile's canonicial path: " + newFile.getCanonicalPath()); FileWriter newWriter = new FileWriter(newFile);
    newWriter.write("new test data");
    newWriter.flush();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }输出:f's abstract path:testFile.txt
    f.getAbsolutePath():D:\springWorkSpace\Fortest\testFile.txt
    f.getCanonicialPath():D:\springWorkSpace\Fortest\testFile.txt
    abstractf's abstract path:..\testFile.txt
    abstractf.getAbsolutePath():D:\springWorkSpace\Fortest\..\testFile.txt
    abstractf.getCanonicialPath():D:\springWorkSpace\testFile.txt
    emptyf's abstract path:
    emptyf.getAbsolutePath():D:\springWorkSpace\Fortest
    emptyf.getCanonicialPath():D:\springWorkSpace\Fortest
    -------------
    newFile's abstract path: \newFile.txt
    newFile's absolute path: D:\newFile.txt
    newFile's canonicial path: D:\newFile.txt
      

  4.   

    我的理解是两个路径相组合形成新的路径,如果父路为空那么就用子路径,如果子路径为空父路径存在那么就是以父路径实例话。
    import java.io.File;public class FileParent {
    public static void main(String args[]) throws Exception{
    File file = new File("d:") ;
    File file1 = new File(file,"test.txt") ;
    File file2 = null ;
    File file3 = new File(file2,"d:\\test.txt") ;
    System.out.println("绝对路径:" + file1.getAbsolutePath() 
    +"--->父路径: " +file1.getParent()) ;
    System.out.println("绝对路径:" + file3.getAbsolutePath() 
    + "--->父路径: " +file3.getParent()) ;
    }
    }
    执行结果:
    绝对路径:d:\test.txt--->父路径: d:\
    绝对路径:d:\test.txt--->父路径: d:\