java创建文件 linux?linux java
Java File 操作在windows和linux的不同
具体的创建方法参照下面的实例:publicclassFileTest{publicstaticvoidmain(String[]args){//根据系统的实际情况选择目录分隔符(windows下是,linux下是/)Stringseparator=File.separator;Stringdirectory="myDir1"+separator+"myDir2";//以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的//linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用\来表示反斜杠)//Stringdirectory="myDir1/myDir2";StringfileName="myFile.txt";//在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件Filef=newFile(directory,fileName);if(f.exists()){//文件已经存在,输出文件的相关信息System.out.println(f.getAbsolutePath());System.out.println(f.getName());System.out.println(f.length());}else{//先创建文件所在的目录f.getParentFile().mkdirs();try{//创建新文件f.createNewFile();}catch(IOExceptione){System.out.println("创建新文件时出现了错误。。。");e.printStackTrace();}}}}
如何利用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文件。
如何用java在指定目录下创建一个文件夹
具体的创建方法参照下面的实例:
public class FileTest{
publicstaticvoidmain(String[]args){
//根据系统的实际情况选择目录分隔符(windows下是,linux下是/)
Stringseparator=File.separator;
Stringdirectory="myDir1"+separator+"myDir2";
//以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的
//linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用\来表示反斜杠)
//Stringdirectory="myDir1/myDir2";
StringfileName="myFile.txt";
//在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件
Filef=newFile(directory,fileName);
if(f.exists()){
//文件已经存在,输出文件的相关信息
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
}else{
//先创建文件所在的目录
f.getParentFile().mkdirs();
try{
//创建新文件
f.createNewFile();
}catch(IOExceptione){
System.out.println("创建新文件时出现了错误。。。");
e.printStackTrace();
}
}
}
}