File.createNewFile()throws IOException没有这样的文件或目录 [英] File.createNewFile() thowing IOException No such file or directory

查看:190
本文介绍了File.createNewFile()throws IOException没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个写入日志文件的方法。如果文件存在,它应该追加到它,如果没有,那么我希望它创建一个新的文件。

  if(!file .exists()&&!file.createNewFile()){
System.err.println(输出文件错误:+ outFile
+\\\
无法创建新文件。 ;
继续;



$ b我有这个来检查文件是否可以被创建。文件是一个java.io.File对象。 createNewFile抛出一个IOException:没有这样的文件或目录。自从我几个星期前写这个方法以来,这个方法一直运行的很好,最近才开始这样做,尽管我不知道我能改变什么。我已经检查,目录存在,我有写权限,但后来我认为它应该只是返回false,如果由于任何原因无法生成文件。



有什么我失踪,以得到这个工作?通常这是你最近改变的东西,首先关闭你的样品代码是如果不是文件存在,而不是创建新文件 - 你正试图代码的东西 - 这是什么?

然后,看一个目录列表,看看它是否实际上是存在的,并在文件对象上执行println / toString()和getMessage()以及打印堆栈跟踪。

然后,从零知识再次,并从每一步,你用来到这里起点的因素。这可能是一个呃,你在代码概念化(因为它正在工作)的地方卡在那里 - 你只是回顾每一步的细节,你会发现它。


I have a method that writes to a log file. If the file exists it should append to it, if not then I want it to create a new file.

if (!file.exists() && !file.createNewFile()) {
    System.err.println("Error with output file: " + outFile
        + "\nCannot create new file.");
    continue;
}

I have that to check that a file can be created. file is a java.io.File object. createNewFile is throwing an IOException: No such file or directory. This method has been working perfectly since I wrote it a few weeks ago and has only recently starting doing this although I don't know what I could have changed. I have checked, the directory exists and I have write permissions for it, but then I thought it should just return false if it can't make the file for any reason.

Is there anything that I am missing to get this working?

解决方案

normally this is something you changed recently, first off your sample code is if not file exists and not create new file - you are trying to code away something - what is it?

Then, look at a directory listing to see if it actually exists and do a println / toString() on the file object and getMessage() on the exception, as well as print stack trace.

Then, start from zero knowledge again and re factor from the get-go each step you are using to get here. It's probably a duh you stuck in there somewhere while conceptualizing in code ( because it was working ) - you just retrace each step in detail, you will find it.

这篇关于File.createNewFile()throws IOException没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆