|
我们还需要一个私有方法转换路径。
/**
* 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);
} |
2.3 打包
将java.io.File编译并打包成jar文件。 filemap_1_4.jar
2.4 设置调试环境。
在你需要调试环境里不需要把这个jar文件发入classpath,但需要在VM arguments里加上
-Xbootclasspath/p:C:\MyProject\filemap_1_4.jar
-Dfilemap=C:\MyProject\filemap.properties
3.测试
编写测试程序
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
FileReader filereader;
try {
//打印/usr/WebSphere/AppServer/InstallApp/Test.java
filereader = new FileReader(
"/usr/WebSphere/AppServer/InstallApp/Test.java");
BufferedReader bufferedreader = new BufferedReader(filereader);
String line=null;
while((line=bufferedreader.readLine())!=null)
System.out.println(line);
//遍历/usr/WebSphere/AppServer/InstallApp/Test.java所在的目录下所有文件,并打印文件名。
File fl=new File("/usr/WebSphere/AppServer/InstallApp/Test.java");
String path=fl.getParent();
String[] files=new File(path).list();
for(int i=0;i System.out.println(files);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} | |