java linux 文件,java打包jar

很多朋友对于java linux 文件和java打包jar不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!

用java如何读取linux中的某个文件

java是跨平台语言,在linux上读文件跟在windows上读文件是一样的只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。

如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi/etc/profile,在文件末尾加上

export LANG="zh_CN.GBK"

export LC_ALL="zh_CN.GBK"

编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");

文件操作的核心代码请参考下面代码:

String path="/home/";

path="/home/multiverse/Repository/PMEPGImport";

File file=new File(path);

File[] tempList= file.listFiles();

for(int i= 0; i< tempList.length; i++){

if(tempList[i].isFile()){

//FileInputStream fis= new FileInputStream("fileName");

//InputStreamReader isr= new InputStreamReader(fis,"utf-8");

StringBuffer buffer= new StringBuffer();

String text;

BufferedReader input= new BufferedReader(new FileReader(tempList[i]));

while((text= input.readLine())!= null)

buffer.append(text+"/n");}

if(tempList[i].isDirectory()){

System.out.println("文件夹:"+tempList[i]);

}

}

java程序怎样读取linux系统下的文件

java是跨平台语言,在linux上读文件跟在windows上读文件是一样的只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。

如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi/etc/profile,在文件末尾加上

export LANG="zh_CN.GBK"

export LC_ALL="zh_CN.GBK"

编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");

文件操作的核心代码请参考下面代码:

String path="/home/";

path="/home/multiverse/Repository/PMEPGImport";

File file=new File(path);

File[] tempList= file.listFiles();

for(int i= 0; i< tempList.length; i++){

if(tempList[i].isFile()){

//FileInputStream fis= new FileInputStream("fileName");

//InputStreamReader isr= new InputStreamReader(fis,"utf-8");

StringBuffer buffer= new StringBuffer();

String text;

BufferedReader input= new BufferedReader(new FileReader(tempList[i]));

while((text= input.readLine())!= null)

buffer.append(text+"/n");}

if(tempList[i].isDirectory()){

System.out.println("文件夹:"+tempList[i]);

}

}

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

虽然,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本地调试并运行呢?一下,我给出一个小小方案。/*** 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文件。

阅读剩余
THE END