Java创建目录的问题

发布于 2024-07-27 14:18:01 字数 1045 浏览 2 评论 0原文

我正在尝试用 Java 创建一个目录。 我认为我已经正确提供了所有必要的内容,以便我创建目录,但它没有创建。 您可以从下面的代码和相应的输出中看到,我构成新目录路径的每个元素都应该是正确且有效的。 然而,tDir.mkdir(); 似乎没有执行任何操作,因此 success 变量始终为 false。 我不明白为什么。 先感谢您。

System.out.println("experimentDir: " + experimentDir);
System.out.println("item.getName(): " + item.getName());
System.out.println("dirName: " + dirName);
String tDirStr = experimentDir + "/" + item.getName() + "All/" 
    + dirName + "DataAll";
System.out.println("tDirStr: " + tDirStr);
File tDir = new File(tDirStr);
if (tDir.exists()) {
      System.out.println("EXISTS!!!");
} else {
      boolean success = tDir.mkdir();
      if(success) {
            System.out.println("Dir created");
      } else {
            System.out.println("No dir created!");
      }

输出:

 experimentDir: /home/Documents/datasets/test-experiments
 item.getName(): PosNegReviews
 dirName: test
 tDirStr: /home/Documents/datasets/test-experiments/PosNegReviewsAll/testDataAll
 No dir created!

I am trying to create a directory in Java. I think I have provided correctly all necessary things so that I make the directory, but it is not created. You can see from my code below and the corresponding output that every element from which I compose the path of the new directory should be correct and valid.
It seems, however, that tDir.mkdir(); is not doing anything, and therefore the success variable is always false. I cannot understand why. Thank you in advance.

System.out.println("experimentDir: " + experimentDir);
System.out.println("item.getName(): " + item.getName());
System.out.println("dirName: " + dirName);
String tDirStr = experimentDir + "/" + item.getName() + "All/" 
    + dirName + "DataAll";
System.out.println("tDirStr: " + tDirStr);
File tDir = new File(tDirStr);
if (tDir.exists()) {
      System.out.println("EXISTS!!!");
} else {
      boolean success = tDir.mkdir();
      if(success) {
            System.out.println("Dir created");
      } else {
            System.out.println("No dir created!");
      }

Output:

 experimentDir: /home/Documents/datasets/test-experiments
 item.getName(): PosNegReviews
 dirName: test
 tDirStr: /home/Documents/datasets/test-experiments/PosNegReviewsAll/testDataAll
 No dir created!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-08-03 14:18:01

如果您想创建多个(嵌套)目录,您应该使用 mkdirs()(注意s)。

If you want to create multiple (nested) directories you should use mkdirs() (note the s).

花开柳相依 2024-08-03 14:18:01

您可能需要创建任何不存在的父目录。 尝试 File.mkdirs()。

you may be needing to create any parent directory that dont exist. try File.mkdirs().

只是一片海 2024-08-03 14:18:01
public class Test1{
    public static void main(String[] args)
    {
        String path="c:\\dir1\\dir2\\dir3\\dir4";
        File dir=new File(path);
        if(!dir.exists()){
            dir.mkdirs();
        }
    }
}

上面的代码将在 C:\dir1\dir2\dir3 中创建 dir4。 如果父文件夹不存在,那么它也会创建。

public class Test1{
    public static void main(String[] args)
    {
        String path="c:\\dir1\\dir2\\dir3\\dir4";
        File dir=new File(path);
        if(!dir.exists()){
            dir.mkdirs();
        }
    }
}

above code will create dir4 inside C:\dir1\dir2\dir3. If parent folder does not exist, then it will also create.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文