`

File类createNewFile时的一个注意点

阅读更多

 

 

今天体会到一个陷阱,刚开始还觉得是JDKbug呢,呵呵。

 

是这样的,有FilecreateNewFile方法是,在Windows系统上可以创建成功,而在Linux不行了,第一感觉是JDKbug,而且只会在Linux上出现的取决于。

 

原来一番实验后,发现原来我Windows系统上已经有父目录了,而Linux上没有,这样一来,Linux上肯定会失败了。

 

写下来记在这里。

 

实验代码如下:

 

 

 

public static void main(String[] args) throws IOException {
		String filePath = "."+File.separator +"farParentfile" + File.separator + "parentFile"+File.separator + "test.jsp";
		
		File file = new File(filePath);
		
		
		if (!file.getParentFile().exists()) {
			if (!file.getParentFile().mkdirs()) {
			}
		}

		
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("exist: " +file.exists() );
		
		System.out.println("path: " + file.getCanonicalPath());
	}
 

 

0
0
分享到:
评论

相关推荐

    java临时文件重命名-Java程序重命名文件或目录.pdf

    证明这⼀点的代码⽚段如下-try { File file1 = new File("demo1.txt"); File file2 = new File("demo2.txt"); file1.createNewFile(); file2.createNewFile(); boolean flag = file1.renameTo(file2); System.out....

    java文件操作大全.txt

    file.createNewFile(); } /** *//**创建新文件 * @param path 目录 * @param filename 文件名 * @throws IOException */ public void createFile(String path,String filename) throws IOException...{ ...

    IO输入输出体系.rar

    File.pathSwparator 与系统有关的路径分隔符,它被表示为一个字符串 windows为 分号";" File.pathSwparatorChar 与系统有关的路径分割符,它被表示为一个字符 File.separator 与系统有关的 默认名称分隔符 ,它被...

    3Java SE(下).doc

    * 该方法会获取当前目录下的所有子项,每一个子项无非还是文件或目录,所 以,可以用若干File对象表示每一个子项,最终将他们存入一个数组返回。 */ File[] subs = dir.listFiles(); for(File sub...

    Java:二进制方式读取文件

    // file.createNewFile(); FileOutputStream out = new FileOutputStream(dec); byte buffer[] = new byte[1024]; int count,i; while((count=in.read(buffer))!=-1){ // for循环保证只写入count个byte...

    java文件读写操作

    file.createNewFile(); FileOutputStream out=new FileOutputStream(file,true); for(int i=0;i;i++)......{ StringBuffer sb=new StringBuffer(); sb.append("这是第"+i+"行:前面介绍的各种方法都不关用,为...

    Linux下中文文件名乱码问题的详解 - CSDN博客1

    Linux下中文文件名乱码问题的详解 - CSDN博客原创2016年06月12日 09:59:40标签:压缩 / linux / 乱码Linux下中文文件名乱码

    提款机系统源代码

    this.dataFile.createNewFile(); } this.fw = new FileWriter(this.dataFile,true); this.bw = new BufferedWriter(this.fw); } catch (IOException io) { System.err.println("Cannot open file...

    IO输入输出留

    创建一个 File 封装对象 可以不存在 方法 -------------------------------------------- 文件、目录属性方法 文件、目录属性 canRead() canWrite() canExecute() 是否可读可写可执行...

    DVD项目功能实现

    // 第一次运行时,文件中没有数据,会抛异常 EOF } catch(EOFException e){ System.err.println("首次运行,不要紧张"); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) ...

    src04 Byte

    个人对于java.lang.Byte的分析

    java中文件操作大全

    10. file.createNewFile(); 11. FileOutputStream out=new FileOutputStream(file); 12. int c; 13. byte buffer[]=new byte[1024]; 14. while((c=in.read(buffer))!=-1)...{ 15. for(int i=0;i;i++) ...

    ftp实现简单的文件传输

    file.createNewFile(); } System.out.println("文件长度:"+(int)file.length()); s=ss.accept(); System.out.println("建立socket连接"); DataInputStream dis=new DataInputStream(new ...

    Java判断文件是否存在

    file.createNewFile(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } }  2、判断文件夹是否存在,不存在创建文件夹 Filefile=newFile("C:\Users\QPING\...

    octokit-create-new-file:在新分支上创建新文件并打开新的拉取请求

    在新的分支上创建一个新文件,并打开一个新的拉取请求。 用法 const createNewFile = require ( 'octokit-create-new-file' ) const newPr = await createNewFile ( octokit , { pr : { title : 'This is a new ...

    上传文件到Web服务器

    首先在“文件名”文本框中输入保存到服务器上的文件名,...LzwUploadBean类中使用了File对象的separatorChar()方法、File对象的createNewFile()方法及FileOutputStream对象的write()方法。下面介绍这3种方法。

    整合的开源头像上传剪裁组件(flash)兼容性好

    file1.createNewFile(); } // 将BufferedImage变量写入文件中。 ImageIO.write(br, "jpg", file1); "imgUrl": "test.jpg", 初始的照片 function uploadevent(status) { 接收来自action的返回值,json个数 map.put...

    Android Studio实现文件管理器,源代码详细注释

    搜索的原理很简单,就是关键词筛选,`contains()`方法判断是否包含关键词,将for循环得到的列表中每一个文件对象都放入File数组中,再重新设置适配器。 `file.createNewFile()`方法用来新建文件,

    java写的记事本

    file.createNewFile(); bw.close(); } catch (IOException e) { JOptionPane.showConfirmDialog(container, "保存文件失败!", "ERROR", JOptionPane....

    AccountMS.rar

    // File dbFile = new File("/data/data/...// dbFile.createNewFile(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // dbFile.delete();

Global site tag (gtag.js) - Google Analytics