Java - mkdir() 不写入目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12126987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - mkdir() not writing directory
提问by Nick
I am trying to create a directory but it seems to fail every time? I have checked that it is not a permission issue too, I have full permission to write to that directory. Thanks in advance.
我正在尝试创建一个目录,但似乎每次都失败?我已经检查过这也不是权限问题,我有写入该目录的完全权限。提前致谢。
Here is the code:
这是代码:
private void writeTextFile(String v){
try{
String yearString = convertInteger(yearInt);
String monthString = convertInteger(month);
String fileName = refernce.getText();
File fileDir = new File("C:\Program Files\Sure Important\Report Cards\" + yearString + "\" + monthString);
File filePath = new File(fileDir + "\"+ fileName + ".txt");
writeDir(fileDir);
// Create file
FileWriter fstream = new FileWriter(filePath);
try (BufferedWriter out = new BufferedWriter(fstream)) {
out.write(v);
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private void writeDir(File f){
try{
if(f.mkdir()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
e.printStackTrace();
}
}
public static String convertInteger(int i) {
return Integer.toString(i);
}
Calendar cal = new GregorianCalendar();
public int month = cal.get(Calendar.MONTH);
public int yearInt = cal.get(Calendar.YEAR);
Here is the output:
这是输出:
Directory is not created
Error: C:\Program Files\Sure Important\Report Cards1232.txt (The system cannot find the path specified)
回答by La bla bla
It's possibly because File.mkdir
creates the directory only if the parent directory exists.
Try using File.mkdirs
which creates all the necessary directories.
这可能是因为File.mkdir
仅当父目录存在时才创建目录。尝试使用File.mkdirs
which 创建所有必要的目录。
Here's the code which worked for me.
这是对我有用的代码。
private void writeDir(File f){
try{
if(f.mkdirs()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
// Demo purposes only. Do NOT do this in real code. EVER.
// It squashes exceptions ...
e.printStackTrace();
}
}
The only change I made was to change f.mkdir()
to f.mkdirs()
and it worked
我所做的唯一更改是更改f.mkdir()
为f.mkdirs()
并且它起作用了
回答by Stephen C
I think that @La bla bla has nailed it, but just for completeness, here are all of the things that I can think of that couldcause a call to File.mkdir()
to fail:
我认为@La bla bla 已经搞定了,但为了完整起见,以下是我能想到的所有可能导致调用File.mkdir()
失败的事情:
- A syntax error in the pathname; e.g. an illegal character in a file name component
- The directory to contain the final directory component does not exist.
- There is already something with that name.
- You don't have permission to create a directory in the parent directory
- You don't have permission to do a lookup in some directory on the path
- The directory to be created is on a read-only file system.
- The file system gave a hardware error or network related error.
- 路径名中的语法错误;例如文件名组件中的非法字符
- 包含最终目录组件的目录不存在。
- 已经有同名的东西了。
- 您无权在父目录中创建目录
- 您无权在路径上的某个目录中进行查找
- 要创建的目录位于只读文件系统上。
- 文件系统出现硬件错误或网络相关错误。
(Obviously, some of these possibilities can be quickly eliminated in the context of this question ...)
(显然,在这个问题的背景下,其中一些可能性可以很快消除......)