gpt4 book ai didi

java - 在 OS X 上创建文件夹

转载 作者:行者123 更新时间:2023-12-01 13:00:38 24 4
gpt4 key购买 nike

我有一个代码可以在 Mac 操作系统中创建文件夹

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.File;
import java.net.URL;

/**
*
* @author kishan
*/
public class CreatingMacFile {
public static void main(String[] args){

boolean check = new CreatingMacFile().makefile();
if (check) {
System.out.println("file created");
}else{
System.out.println("file is not created");
}
}


public boolean makefile() {
try {

String resource = CreatingMacFile.class.getName().replace(".", File.separator) + ".class";
URL fileURL = ClassLoader.getSystemClassLoader().getResource(resource);

String path = new File(fileURL.toURI()).getParent();
System.out.println("this is path that we getting: "+path);
String mySubFolder = "subFolder";
File newDir = new File(path + File.separator + mySubFolder);

System.out.println("File Path: "+newDir.getAbsolutePath());

boolean success = newDir.mkdir();

if (success) {
return true;
}else{
return false;
}

} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}

但我收到 noClassFound 错误

我缺少什么?

堆栈跟踪

Exception in thread "main" java.lang.NoClassDefFoundError: CreatingMacFile
Caused by: java.lang.ClassNotFoundException: CreatingMacFile
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

最佳答案

Class<?> c = ...
File file = new File("/tmp/", c.getName().replaceAll("\\.", File.separator));
file.mkdirs();

注意 mkdirs() 方法(以 s 结尾)。 HTH。

关于java - 在 OS X 上创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23541008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com