File类 File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。 其中“抽象路径”具体指的是什么,最好举个例子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 file.getAbsoluteFile() 打印出来自己看看 我错了, 无视上面吧应该就是new parent的时候所用的路径吧 根据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.txtf.getAbsolutePath():D:\springWorkSpace\Fortest\testFile.txtf.getCanonicialPath():D:\springWorkSpace\Fortest\testFile.txtabstractf's abstract path:..\testFile.txtabstractf.getAbsolutePath():D:\springWorkSpace\Fortest\..\testFile.txtabstractf.getCanonicialPath():D:\springWorkSpace\testFile.txtemptyf's abstract path:emptyf.getAbsolutePath():D:\springWorkSpace\Fortestemptyf.getCanonicialPath():D:\springWorkSpace\Fortest-------------newFile's abstract path: \newFile.txtnewFile's absolute path: D:\newFile.txtnewFile's canonicial path: D:\newFile.txt 我的理解是两个路径相组合形成新的路径,如果父路为空那么就用子路径,如果子路径为空父路径存在那么就是以父路径实例话。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:\ 关于java重构,list作为参数如何写才是对? 关于线程问题 程序设计 一个用JSP+mysql建立用户登陆,查询.删除等功能的程序.各位高手来帮忙看下吧 请看过Thinking in java的请指点 无法连接sql server2005 socket连接的服务器端怎么控制Client的IP地址? 求教把class文件打包成可双激执行的jar或者exe的方法~~搞了一天一夜了~~~急得想死了!UP者有分~~ 这段代码错在哪儿? 关于电影院座次的问题,求各位大神帮忙写一个Java的代码。 JAXP处理XML文件的时候,把空格也当作text节点? 在一个九宫格上随机填上'X','O'图片
打印出来自己看看
应该就是new parent的时候所用的路径吧
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
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:\