我们还需要一个私有方法转换路径。
/**
* Get Virutal Path string
* @param name 原UNIX/Linux路径。
* @return 新windows路径。
*/
private String getVirtualPath(String name){
Iterator sources=filemaps.keySet().iterator();
while(sources.hasNext()){
String source=(String)sources.next();
if(name.startsWith(source)==true){
//当原路径包含filemaps里某一个source路径时,将原路径转换为新的target路径。
String target=(String)filemaps.get(source);
name=target name.substring(source.length());
}
}
return name;
}
好了,现在准备在Java.io.File里调用这两个方法。
/**
* Creates a new File instance by converting the given
* pathname string into an abstract pathname. If the given string is
* the empty string, then the result is the empty abstract pathname.
*
* @param pathname A pathname string
* @throws NullPointerException
* If the pathname argument is null
*/
public File(String pathname) {
//new function
initmaps();
if (pathname == null) {
throw new NullPointerException();
}
//new function
pathname=getVirtualPath(pathname);
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
public File(String parent, String child) {
//new function
initmaps();
if (child == null) {
throw new NullPointerException();
}
//new function
child=getVirtualPath(child);
parent=getVirtualPath(parent);
if (parent != null) {
if (parent.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
this.path = fs.resolve(fs.normalize(parent),
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}
public File(File parent, String child) {
//new function
initmaps();
child=getVirtualPath(child);
if (child == null) {
throw new NullPointerException();
}
if (parent != null) {
if (parent.path.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
String parentpath=getVirtualPath(parent.path);
this.path = fs.resolve(parent.path,
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}