赛迪网 > IT技术 Linux > 实用技巧
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

如何利用Java虚拟Unix/Linux的文件路径

发布时间:2007.09.25 06:27     来源:赛迪网    作者:kit

大部分的java程序应用于UNIX/Linux系统,而绝大部分的开发是在Windows下。虽然,java可以运行在anywhere, 但毕竟还有很多环境配置问题。

例如在UNIX下,你需要将某些配置文件的路径写入到另一个配置文件。 也许有很多局限,使你必须写入绝对路径。

在config.properties里写入

logs = /logs/app/db/logs.properties

configs=/usr/WebSphere/AppServer/installedApps/appname/earname/warname/WEB-INF/properties/myconfig.properties

在开发阶段,你是否愿意在你的Windows开发机上建立上面这样的目录,或者逐个修改这个路径呢? 尤其在已有的系统下,为了开发新的功能,构筑开发环境时,这种配置文件路径的修改是相当花时间的。 并且,在Release时,你必须要使用Ant工具批量修改这些配置文件。 但我可以说,大部分项目只有给生产和系统集成测试环境才会配置Ant工具。而在低级别的测试环境下,你只能手动更改。 那么如何才能不修改任何文件可以再windows本地调试并运行呢?

一下,我给出一个小小方案。

1. 重写java.io.File类

先不要向我丢香蕉皮, 重写java.io.File并不一定要变动rt.jar文件。 jvm支持pretend,也就是伪装,我可以把我重写的java.io.File在运行期时代替rt.jar原有的java.io.File类。 想了解更详细的信息可以在 JAVA_HOME里找这个文件:[ JAVA_HOME]\bin\client\Xusage.txt

-Xbootclasspath/p:

prepend in front of bootstrap class path

在调试时,我就是要用这个参数。假设,我把重写的java.io.File类文件打包为filemap_1_4.jar。调试时,我就可以运行 java -Xbootclasspath/p:D:\MyProject\FileMap/filemap_1_4.jar -cp ...

这样,在我调用的所有类里,涉及到文件或文件系统功能时,都调用D:\MyProject\FileMap/filemap_1_4.jar 下面的java.io.File而不是rt.jar.

2. 功能实现

2.1 文件目录映射关系

为了增加一些灵活性, 我使用一个目录映射文件,来定义UNIX/LINUX文件路径和Windows文件路径的映射关系。

例如,filemap.properties

/usr/WebSphere/AppServer/installedApps/appname/earname/warname/=C:/MyProject/

/logs/app/db/=c:/MyProject/logs

当程序要读取/usr/WebSphere/AppServer/installedApps/appname/earname/warname/WEB-INF/properties/myconfig.properties

文件时,java.io.File会映射到C:/MyProject/WEB-INF/properties/myconfig.properties。

2.2 java.io.File更改

增加一个静态变量 private static HashMap filemaps=null;用来保存映射关系。

增加一个私有方法 initmaps初始化 filemaps

/**
* read filemap.propreties to initialize file map.
*/
private void initmaps(){
if(filemaps==null){
filemaps=new HashMap();
String filemap=System.getProperty("filemap"); 
//获得filemap.properties文件路径,需要在jvm运行时传入-Dfilemap=[filemap.properties全路径名],
不要试图使用 classloader.getResource(), 因为getResource里也会使用java.io.File,会产生jvm异常。

if(filemap==null || filemap=="")
return;
this.path = fs.normalize(filemap); 
//准备读取filemap.properties文件。因为使用FileInputStream时,
需要传入一个java.io.File对象,在这暂 时把this.path设为filemap.properties的路径。

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); //保存到filemaps静态对象里。
}
} catch(FileNotFoundException e1){
return;
} catch(IOException e2){
return;
}
} 
}

我们还需要一个私有方法转换路径。

/**
* 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();
}
}
}

(责任编辑:云子)


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· 如何通过串口来维护Linux操作系统的主机 (09-24) · SuSE Linux操作系统下Apache的设定方法 (09-24)
· 制作自己个性的Linux启动GRUB启动背景图片 (09-24) · Linux头脑风暴Freemind vym rpm安装方法 (09-24)
· Linux内核更新:Linux Kernel 2.6.22.7 (09-24) · Linux系统集群 安装配置排错的基本方法 (09-24)
· 新手学堂:嵌入式系统编程中的代码优化 (09-24) · Openoffice各子模块的编译顺序概括介绍 (09-24)
· Ubuntu流媒体的下载与网页内嵌媒体播放 (09-24) · CentOS4.4用VSFTPD架设FTP服务器的方法 (09-24)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
[政务][电信][金融][农业][制造业][中小企业]
[CIO][ERP][协同][IT管理][中间件][电子商务]
[政策][地方][专家][评估][辞典][博客][社区]
· 专家:从第三方法律服务看我国信息化法制建设
· 信息化的边际效益分析:民营企业不上ERP会怎样
· 特别专题:排队时代电子银行如何补上行业短板
· 股市洪峰背后的IT:信息系统如何应对负载考验
· 案例分析:计划不如变化快 IT该不该实施预算制
· 北京:公交卡用途增多可购物买药 不推行实名制
  博客·论坛 ·曾剑秋·项立刚·Java学习·网管