¤j³¡¤Àªºjavaµ{¦¡À³¥Î©óUNIX/Linux¨t²Î¡A¦Óµ´¤j³¡¤Àªº¶}µo¬O¦bWindows¤U¡CÁöµM¡Ajava¥i¥H¹B¦æ¦banywhere, ¦ý²¦³ºÁÙ¦³«Ü¦hÀô¹Ò°t¸m°ÝÃD¡C
¨Ò¦p¦bUNIX¤U¡A§A»Ýn±N¬Y¨Ç°t¸m¤å¥óªº¸ô®|¼g¤J¨ì¥t¤@Ó°t¸m¤å¥ó¡C ¤]³\¦³«Ü¦h§½¡A¨Ï§A¥²¶·¼g¤Jµ´¹ï¸ô®|¡C
¦bconfig.propertiesùؼg¤J
logs = /logs/app/db/logs.properties
configs=/usr/WebSphere/AppServer/installedApps/appname/earname/warname/WEB-INF/properties/myconfig.properties
¦b¶}µo¶¥¬q¡A§A¬O§_Ä@·N¦b§AªºWindows¶}µo¾÷¤W«Ø¥ß¤W±³o¼Ëªº¥Ø¿ý¡A©ÎªÌ³vÓ×§ï³oÓ¸ô®|©O¡H ¤×¨ä¦b¤w¦³ªº¨t²Î¤U¡A¬°¤F¶}µo·sªº¥\¯à¡Aºc¿v¶}µoÀô¹Ò®É¡A³oºØ°t¸m¤å¥ó¸ô®|ªº×§ï¬O¬Û·íªá®É¶¡ªº¡C ¨Ã¥B¡A¦bRelease®É¡A§A¥²¶·n¨Ï¥ÎAnt¤u¨ã§å¶q×§ï³o¨Ç°t¸m¤å¥ó¡C ¦ý§Ú¥i¥H»¡¡A¤j³¡¤À¶µ¥Ø¥u¦³µ¹¥Í²£©M¨t²Î¶°¦¨´ú¸ÕÀô¹Ò¤~·|°t¸mAnt¤u¨ã¡C¦Ó¦b§C¯Å§Oªº´ú¸ÕÀô¹Ò¤U¡A§A¥u¯à¤â°Ê§ó§ï¡C ¨º»ò¦p¦ó¤~¯à¤£×§ï¥ô¦ó¤å¥ó¥i¥H¦Awindows¥»¦a½Õ¸Õ¨Ã¹B¦æ©O¡H
¤@¤U¡A§Úµ¹¥X¤@Ó¤p¤p¤è®×¡C
1. «¼gjava.io.FileÃþ
¥ý¤£n¦V§Ú¥á»¿¼¥Ö¡A «¼gjava.io.File¨Ã¤£¤@©wnÅܰÊrt.jar¤å¥ó¡C jvm¤ä´©pretend¡A¤]´N¬O°°¸Ë¡A§Ú¥i¥H§â§Ú«¼gªºjava.io.File¦b¹B¦æ´Á®É¥N´Àrt.jar즳ªºjava.io.FileÃþ¡C ·Q¤F¸Ñ§ó¸Ô²Óªº¸ê°T¥i¥H¦b JAVA_HOMEùاä³oÓ¤å¥ó¡G[ JAVA_HOME]\bin\client\Xusage.txt
-Xbootclasspath/p:
prepend in front of bootstrap class path
¦b½Õ¸Õ®É¡A§Ú´N¬On¥Î³oӰѼơC°²³]¡A§Ú§â«¼gªºjava.io.FileÃþ¤å¥ó¥´¥]¬°filemap_1_4.jar¡C½Õ¸Õ®É¡A§Ú´N¥i¥H¹B¦æ java -Xbootclasspath/p:D:\MyProject\FileMap/filemap_1_4.jar -cp ...
³o¼Ë¡A¦b§Ú½Õ¥Îªº©Ò¦³ÃþùØ¡A¯A¤Î¨ì¤å¥ó©Î¤å¥ó¨t²Î¥\¯à®É¡A³£½Õ¥ÎD:\MyProject\FileMap/filemap_1_4.jar ¤U±ªºjava.io.File¦Ó¤£¬Ort.jar.
2. ¥\¯à¹ê²{
2.1 ¤å¥ó¥Ø¿ý¬M®gÃö«Y
¬°¤F¼W¥[¤@¨ÇÆF¬¡©Ê¡A §Ú¨Ï¥Î¤@ӥؿý¬M®g¤å¥ó¡A¨Ó©w¸qUNIX/LINUX¤å¥ó¸ô®|©MWindows¤å¥ó¸ô®|ªº¬M®gÃö«Y¡C
¨Ò¦p¡Afilemap.properties
/usr/WebSphere/AppServer/installedApps/appname/earname/warname/=C:/MyProject/
/logs/app/db/=c:/MyProject/logs
·íµ{¦¡nŪ¨ú/usr/WebSphere/AppServer/installedApps/appname/earname/warname/WEB-INF/properties/myconfig.properties
¤å¥ó®É¡Ajava.io.File·|¬M®g¨ìC:/MyProject/WEB-INF/properties/myconfig.properties¡C
2.2 java.io.File§ó§ï
¼W¥[¤@ÓÀRºAÅÜ¼Æ private static HashMap filemaps=null;¥Î¨Ó«O¦s¬M®gÃö«Y¡C
¼W¥[¤@Ó¨p¦³¤èªk initmapsªì©l¤Æ filemaps
/**
* read filemap.propreties to initialize file map.
*/
private void initmaps(){
if(filemaps==null){
filemaps=new HashMap();
String filemap=System.getProperty("filemap");
//Àò±ofilemap.properties¤å¥ó¸ô®|¡A»Ýn¦bjvm¹B¦æ®É¶Ç¤J-Dfilemap=[filemap.properties¥þ¸ô®|¦W]¡A
¤£n¸Õ¹Ï¨Ï¥Î classloader.getResource(), ¦]¬°getResourceùؤ]·|¨Ï¥Îjava.io.File¡A·|²£¥Íjvm²§±`¡C
if(filemap==null || filemap=="")
return;
this.path = fs.normalize(filemap);
//·Ç³ÆÅª¨úfilemap.properties¤å¥ó¡C¦]¬°¨Ï¥ÎFileInputStream®É¡A
»Ýn¶Ç¤J¤@Ójava.io.File¹ï¶H¡A¦b³o¼È ®É§âthis.path³]¬°filemap.propertiesªº¸ô®|¡C
this.prefixLength = fs.prefixLength(this.path);
Properties pro=new Properties();
try {
pro.load(new FileInputStream(this)); //Ū¨úfilemap.properties.
Enumeration enumeration=pro.propertyNames();
while(enumeration.hasMoreElements()){
String sourcepath=(String)enumeration.nextElement();
String targetpath=pro.getProperty(sourcepath);
filemaps.put(sourcepath, targetpath); //«O¦s¨ìfilemapsÀRºA¹ï¶HùØ¡C
}
} catch(FileNotFoundException e1){
return;
} catch(IOException e2){
return;
}
}
}
|
§ÚÌÁÙ»Ýn¤@Ó¨p¦³¤èªkÂà´«¸ô®|¡C
/**
* Get Virutal Path string
* @param name ìUNIX/Linux¸ô®|¡C
* @return ·swindows¸ô®|¡C
*/
private String getVirtualPath(String name){
Iterator sources=filemaps.keySet().iterator();
while(sources.hasNext()){
String source=(String)sources.next();
if(name.startsWith(source)==true){
//·íì¸ô®|¥]§tfilemapsùجY¤@Ósource¸ô®|®É¡A±Nì¸ô®|Âà´«¬°·sªºtarget¸ô®|¡C
String target=(String)filemaps.get(source);
name=target+name.substring(source.length());
}
}
return name;
}
|
¦n¤F¡A²{¦b·Ç³Æ¦bjava.io.Fileùؽեγo¨âÓ¤èªk¡C
/**
* 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 ¥´¥]
±Njava.io.File½sĶ¨Ã¥´¥]¦¨jar¤å¥ó¡C filemap_1_4.jar
2.4 ³]¸m½Õ¸ÕÀô¹Ò¡C
¦b§A»Ýn½Õ¸ÕÀô¹Òùؤ£»Ýn§â³oÓjar¤å¥óµo¤Jclasspath¡A¦ý»Ýn¦bVM arguments¨½¥[¤W
-Xbootclasspath/p:C:\MyProject\filemap_1_4.jar -Dfilemap=C:\MyProject\filemap.properties
3.´ú¸Õ
½s¼g´ú¸Õµ{¦¡
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 {
//¦C¦L/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);
//¹M¾ú/usr/WebSphere/AppServer/InstallApp/Test.java©Ò¦bªº¥Ø¿ý¤U©Ò¦³¤å¥ó¡A¨Ã¦C¦L¤å¥ó¦W¡C
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();
}
}
}
|
¡]³d¥ô½s¿è¡G¶³¤l¡^