「mkdir()java」mkdirjava

博主:adminadmin 2022-12-07 06:00:09 67

本篇文章给大家谈谈mkdir()java,以及mkdirjava对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 文件夹中如何创建文件夹?

File类里面有两个方法可以实现:

一个是mkdir():创建此抽象路径名指定的目录。

另外一个是mkdirs(): 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。

比如你想在A文件夹创建一个B文件夹,并在B文件夹下创建c和D文件夹,可以用下面的代码实现:

import java.io.File;

public class Test {

public static void main(String args[]) {

File file = new File("D:\\A\\B\\C");

file.mkdirs();

file = new File("D:\\A\\B\\D");

file.mkdir();

}

}

希望对你有帮助。。。。仍有问题可以HI我。。。

java.io.File中mkdir和mkdirs的区别?

mkdirs可以创建指定目录以及所有的父目录,创建此抽象路径名指定的目录,包括所有必需但不存在的父目录,也就是可以在不存在的目录中创建文件夹。

mkdir创建此抽象路径名指定的目录,也就是只能在已经存在的目录中创建创建文件夹。

mkdirs()可以建立多级文件夹,mkdir()只会建立一级的文件夹, 如下:

File file=new File("/tmp/one/two/three");

file.mkdirs();

执行后, 会建立tmp/one/two/three四级目录

file..mkdir();

则不会建立任何目录, 因为找不到/tmp/one/two目录, 结果返回false

Java创建文件夹并上传文件到该文件夹:

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;//jar包

//省略get/set方法

private String err = "";

private String msg;              //返回信息

private File filedata;           //上传文件

private String filedataFileName; //文件名

//这里是上传的路径,获取路径用到是struts2的方法

String saveRealFilePath = ServletActionContext.getServletContext().getRealPath("/img");

System.out.println(saveRealFilePath );//打印上传的路径

File fileDir = new File(saveRealFilePath);

if (!fileDir.exists()) { //如果不存在 则创建

fileDir.mkdirs();

}

File savefile = new File(saveRealFilePath , filedataFileName);

try {

FileUtils.copyFile(filedata, savefile); //复制

} catch (IOException e) {

err = "错误"+e.getMessage();

e.printStackTrace();

}

java file类方法mkdir()疑问?

可以先切换到指定的文件夹路径下,之后直接通过mkdir方法进行文件夹创建。举例:

String path = "d:/oldfilepath";//定义指定文件路径

String newPath = path+"/newpath";//指定新路径

File file = new File(newPath );//定义一个文件流

file.mkdir();//创建文件夹

备注:如果不确定原有文件夹是否存在的情况下,可以通过”mkdirs“创建多层路径。

java.io.File中mkdir和mkdirs的区别

mkdir

public boolean mkdir()

Creates the directory named by this abstract pathname.

Returns:

true if and only if the directory was created; false otherwise

Throws:

SecurityException -

If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method

does not permit the named directory to be create

为这个抽象目录创建目录结构。创建成功返回true,否则返回false。

抛出安全异常

mkdirs

public boolean mkdirs()

Creates the directory

named by this abstract pathname, including any necessary but nonexistent

parent directories. Note that if this operation fails it may have

succeeded in creating some of the necessary parent

directories.

Returns:

true if and only if the directory was created, along with all necessary parent directories; false otherwise

Throws:

SecurityException -

If a security manager exists and its SecurityManager.checkRead(java.lang.String) method

does not permit verification of the existence of the named directory and all necessary parent directories; or if theSecurityManager.checkWrite(java.lang.String) method

does not permit the named directory and all necessary parent directories to be created

根据抽象路径名创建目录结构,包括必要但是不存在的父目录。请注意,如果操作失败,但它可能成功的创建了一些必要的父目录。

如果目录被创建成功返回true,否则返回false。

也就是意味着当目录结构不存在的时候 ”mkdir“ 是不能创建目录结构的,而 “mkdirs” 是可以帮助你创建不存在的目录结构的。但是有个缺点,如果程序报错终止,mkdirs有可能已经为你创建了目录结构。

java mkdir和mkdirs的区别

mkdirs()可以建立多级文件夹,

mkdir()只会建立一级的文件夹,

如下:

new

File("/tmp/one/two/three").mkdirs();

执行后,

会建立tmp/one/two/three四级目录

new

File("/tmp/one/two/three").mkdir();

则不会建立任何目录,

因为找不到/tmp/one/two目录,

结果返回false

java中f.mkdir();是什么意思

创建目录用的。

比如:

public class Example {

public static void main(String args[]) {

File f=new File("f:/example/test");

f.mkdir();

}

就在f:/example/文件下创建了一个目录。运行结果:

关于mkdir()java和mkdirjava的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-07,除非注明,否则均为首码项目网原创文章,转载请注明出处。