在我的程序中,我希望用户根据自己的意愿命名他们的项目。所以我需要的是让程序创建一个名为用户想要的的新文件夹的某种方式。我希望它发生的方式是使用 String ,我可以将其放入已有的代码中来制作新文件夹。
package com.AlexAdams.SimpleHTML.Program;
import java.io.File;
import com.AlexAdams.SimpleHTML.PopUps.SimpleHTMLDirctoryError;
public class MainFrameCreateMenuCreateNewProject {
public static boolean programDirctory = false;
public MainFrameCreateMenuCreateNewProject() {
/*"SimpleHTML" MAIN FILE DIRCTORY*/
String simpleHTMLpath = System.getProperty("user.home") + File.separator + "Documents";
simpleHTMLpath += File.separator + "SimpleHTML";
File SimpleHTML = new File(simpleHTMLpath);
if (SimpleHTML.exists()) {
System.out.println(SimpleHTML + " Dirctory already exists");
programDirctory = true;
} else if (SimpleHTML.mkdirs()) {
System.out.println(SimpleHTML + " Dirctory was created");
programDirctory = true;
} else {
System.out.println(SimpleHTML + " Dirctory was not created");
programDirctory = false;
new SimpleHTMLDirctoryError();
}
if (programDirctory = true) {
/*"SimpleHTML User Defined Project Name"*/
String simpleHTMLprojectpath = System.getProperty("user.home") + File.separator + "SimpleHTML";
simpleHTMLprojectpath += File.separator + ""; // Put the code for the users name of there project
File /*String of user input from JTextField*/ = new File(simpleHTMLprojectpath);
if (/*Code of user definedprojectname*/.exists()) {
System.out.println(" Project name already exists");
} else if (/*Code of user definedprojectname*/.mkdirs()) {
System.out.println(" New project was created");
} else {
System.out.println(" New project could not be created");
}
} else if (programDirctory = false) {
System.out.println(" Project could not be created. This may be due to the fact that there is a error with the programs main file dirctroy.");
}
}
}
答案 0 :(得分:1)
首先,你有一个逻辑错误......
if (programDirctory = true) {
在true
语句对其进行评估之前,将true
分配给programDirctory
,将始终为if
。
最好使用
if (programDirctory) {
同样适用于else if (programDirctory = false)
,但考虑到值为true
或false
,只有两种状态可用,因此您可以将其缩短为...... < / p>
if (programDirctory) {
//...
} else {
//...
}
如果要提供变量值,则需要为构造函数提供参数...
例如......
public MainFrameCreateMenuCreateNewProject(String userPath) {
/*"SimpleHTML" MAIN FILE DIRCTORY*/
String simpleHTMLpath = System.getProperty("user.home") + File.separator + "Documents";
simpleHTMLpath += File.separator + "SimpleHTML";
File SimpleHTML = new File(simpleHTMLpath);
if (SimpleHTML.exists()) {
System.out.println(SimpleHTML + " Dirctory already exists");
programDirctory = true;
} else if (SimpleHTML.mkdirs()) {
System.out.println(SimpleHTML + " Dirctory was created");
programDirctory = true;
} else {
System.out.println(SimpleHTML + " Dirctory was not created");
programDirctory = false;
throw new IOException(SimpleHTML + " does not exist and could not be created");
}
if (programDirctory) {
/*"SimpleHTML User Defined Project Name"*/
String simpleHTMLprojectpath = simpleHTMLpath +
File.separator +
userPath; // Put the code for the users name of there project
File userProject = new File(simpleHTMLprojectpath);
if (userProject.exists()) {
System.out.println(" Project name already exists");
} else if (userProject.mkdirs()) {
System.out.println(" New project was created");
} else {
System.out.println(" New project could not be created");
}
}
}
然后你只需要用......之类的东西来调用它。
new MainFrameCreateMenuCreateNewProject("MyProject");
但对我而言,这似乎有点无意义,因为班级没有做任何事情。更好的解决方案可能是制作静态方法......
public class MainFrameCreateMenuCreateNewProject {
public static void createUserFolder(String userPath) {
boolean programDirctory = false;
//...etc...
}
}
允许你使用......
MainFrameCreateMenuCreateNewProject.createUserFolder("MyProject");
下一个问题是,如果由于某种原因无法创建文件夹该怎么办?你可以返回一个boolean
,但这并不意味着什么,相反,抛出一个IOException
可能更好,例如......
public static void createNewProjectPath(String userPath) throws IOException {
/*"SimpleHTML" MAIN FILE DIRCTORY*/
String simpleHTMLpath = System.getProperty("user.home") + File.separator + "Documents";
simpleHTMLpath += File.separator + "SimpleHTML";
File SimpleHTML = new File(simpleHTMLpath);
if (SimpleHTML.exists()) {
System.out.println(SimpleHTML + " Dirctory already exists");
programDirctory = true;
} else if (SimpleHTML.mkdirs()) {
System.out.println(SimpleHTML + " Dirctory was created");
programDirctory = true;
} else {
System.out.println(SimpleHTML + " Dirctory was not created");
programDirctory = false;
throw new IOException(SimpleHTML + " does not exist and could not be created");
}
if (programDirctory) {
/*"SimpleHTML User Defined Project Name"*/
String simpleHTMLprojectpath = simpleHTMLpath +
File.separator +
userPath; // Put the code for the users name of there project
File userProject = new File(simpleHTMLprojectpath);
if (userProject.exists()) {
System.out.println(" Project name already exists");
} else if (userProject.mkdirs()) {
System.out.println(" New project was created");
} else {
System.out.println(" New project could not be created");
throw new IOException(userProject + " does not exist and could not be created");
}
}
}
但是,说实话,你可以通过做类似的事情来简化代码。
public class MainFrameCreateMenuCreateNewProject {
public static void createNewProjectPath(String userPath) throws IOException {
createNewPath("SimpleHTML");
createNewPath(userPath);
}
public static void createNewPath(String path) throws IOException {
String fullPath = System.getProperty("user.home") + File.separator + "Documents";
fullPath += File.separator + path;
File filePath = new File(fullPath);
if (!filePath.exists() && !filePath.mkdirs()) {
throw new IOException(filePath + " does not exist and could not be created");
}
}
}
答案 1 :(得分:0)
简单地问一下,这就是你想要的吗?
public MainFrameCreateMenuCreateNewProject(String ProjectName) {
/*"SimpleHTML" MAIN FILE DIRCTORY*/
String simpleHTMLpath = System.getProperty("user.home") + File.separator + "Documents";
simpleHTMLpath += File.separator + ProjectName;
File SimpleHTML = new File(simpleHTMLpath);
if (SimpleHTML.exists()) {
System.out.println(SimpleHTML + " Dirctory already exists");
programDirctory = true;
} else if (SimpleHTML.mkdirs()) {
System.out.println(SimpleHTML + " Dirctory was created");
programDirctory = true;
} else {
System.out.println(SimpleHTML + " Dirctory was not created");
programDirctory = false;
throw new SimpleHTMLDirctoryException("Error in creating directory");
}
if (programDirctory) {
/*"SimpleHTML User Defined Project Name"*/
String simpleHTMLprojectpath = System.getProperty("user.home") + File.separator + ProjectName;
simpleHTMLprojectpath += File.separator + ProjectName; // Put the code for the users name of there project
File /*String of user input from JTextField*/ file = new File(simpleHTMLprojectpath);
if (file.exists()) {
System.out.println(" Project name already exists");
} else if (file.mkdirs()) {
System.out.println(" New project was created");
} else {
System.out.println("New project could not be created");
}
} else {
System.out.println(" Project could not be created. This may be due to the fact that there is a error with the programs main file dirctroy.");
}
}
如果是这样,你不能只用一个变量替换String,并将它作为构造函数中的参数传递吗?