File类的构造方法和简单用法举例详见收藏的文章。
目前对File的某些用法仍存在一定疑问,具体代码如下:
private NutchOperateProj() {
try {
File nutchFileRoot = new File(NutchDataDir);
File segmentsDir = new File(nutchFileRoot,"segments");
File indexDir = new File(nutchFileRoot,"index");
//分别打开index
searcher = new IndexSearcher(indexDir.getAbsolutePath());
segments = new FetchedSegments(NutchConfiguration.create(),new Path(segmentsDir.toString()));
}//end try
catch (Exception ex) {
ex.printStackTrace();
}//end catch
}//end Nutchoperate
本语句目的是将路径为nutchFileRoot下的"segments"文件实例化,即该文件以前已经存在,实例化之后便于直接改写。
另外在java中,“目录”也属于文件,实例语句中“NutchDataDir”为存放nutch抓取后文件的路径字符,nutchFileRoot是该目录的File化。segmentsDir是nutch中segments的File实例,即将nutchFileRoot目录下的segments文件实例化。
答案取自百度知道:http://zhidao.baidu.com/question/311796975.html
File(File f, String child)
根据f 抽象路径名和 child 路径名字符串创建一个新 File 实例。
f抽象路径名用于表示目录,child 路径名字符串用于表示目录或文件
f - 父抽象路径名
child - 子路径名字符串
File(String parent, String child)
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
parent 路径名字符串用于表示目录,child 路径名字符串用于表示目录或文件
parent - 父路径名字符串
child - 子路径名字符串
-
追问
-
哥们,File(File f,String child) 在实例化对象时,该怎么写代码??
-
在java中,目录也是一种文件,可以像创建文件一样创建目录文件:
-
1: File f = new File("F:\\java\\rose"); (其中F:\java\rose是一个已经存在的目录),你可以调用file.exists()看目录是否存在,如存在则函数返回true,如不存在你可以调用 file.mkdir(); 创建rose目录。 2: File file=new(f,“file1.txt”);可以实例化F:\java\rose目录下的文件file1.txt(文件已存在),你可以调用file.exists()看文件是否存在,如存在则函数返回true,如不存在你可以调用 file.createNewFile()创建 file1.txt文件。注意,当你实例化一个已经存在的文件时,一定要加上后缀名,如.txt等,否则实例化的文件是不存在的。调用file.exists()返回false. File f = new File("F:\\java\\rose"); (其中F:\java\rose是一个已经存在的目录) File file=new(f,“file1.txt”);可以实例化F:\java\rose目录下的文件file1.txt(文件已存在) 具体解释如上所述。