File类创建文件夹失败,但没有抛出异常 我在写文件管理器的时候有个功能是创建文件夹,第一次创建很正常,但是立马接着创建下一个文件夹就失败了,没有任何异常抛出,求解 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* * @创建文件夹 * */ public boolean mkDir(String path){ File dir = new File(path); if(dir.exists()){ System.out.println("文件夹已经存在!"); return false; } if(!path.endsWith(File.separator)){ path = path + File.separator; if(new File(path).mkdir()){ System.out.println("创建目录:"+path+"成功!"); return true; } } return false; }class AddDir implements ActionListener{ public void actionPerformed(ActionEvent e) { IconNode node = (IconNode) pathTree.getLastSelectedPathComponent(); TreePath lastPath = pathTree.getSelectionPath(); pathTree.fireTreeExpanded(lastPath); String name = JOptionPane.showInputDialog("请输入文件夹的名称:", "新建文件夹 "); if(name!=""){ IconNode newNode = new IconNode(getFolderIcon(),name); newNode.setAllowsChildren(false); System.out.println("创建目录:"+getNodePath(node)+name); if(mkDir(getNodePath(node)+name)){ if(!node.getAllowsChildren()) node.setAllowsChildren(true); dtm.insertNodeInto(newNode, node, node.getChildCount()); pathTree.setSelectionPath(new TreePath(newNode.getPath())); pathTree.scrollPathToVisible(new TreePath(newNode.getPath())); } else{ JOptionPane.showConfirmDialog(null, "文件夹创建失败!","文件夹创建警告 ",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null); } } } } 象你这样写逻辑上是有问题的 if (!path.endsWith(File.separator)) { path = path + File.separator; if (new File(path).mkdir()) { System.out.println("创建目录:" + path + "成功!"); return true; } } return false;如果path以"\"结尾,就不会被创建而直接返回false改成下面这样: public boolean mkDir(String path) { File dir = new File(path); if (dir.exists()) { System.out.println("文件夹已经存在!"); return false; } if (!path.endsWith(File.separator)) { path = path + File.separator; } if (new File(path).mkdir()) { System.out.println("创建目录:" + path + "成功!"); return true; } return false; } 新手问下:SWT登录界面切换 JPasswordField.getPassword().toString()和new String(JPasswordField.getPassword()) 做网站的具体流程是什么? ssh整合程序无法提交数据,分不够就加!! 一个java解释器的问题 *** vj 和 borland j++ builder 哪个好?*** 亲爱的朋友:请多指教! 好消息,近来看看...... 测试ConcurrentHashMap是否读写安全 如何统计代码子目录的单行注释 、多行注释、 空白行 、代码的行数的问题 求一个查找重复字府串的算法 字符串截取成数组的问题,在线等
* @创建文件夹
* */
public boolean mkDir(String path){
File dir = new File(path);
if(dir.exists()){
System.out.println("文件夹已经存在!");
return false;
}
if(!path.endsWith(File.separator)){
path = path + File.separator;
if(new File(path).mkdir()){
System.out.println("创建目录:"+path+"成功!");
return true;
}
}
return false;
}
class AddDir implements ActionListener{
public void actionPerformed(ActionEvent e) {
IconNode node = (IconNode) pathTree.getLastSelectedPathComponent();
TreePath lastPath = pathTree.getSelectionPath();
pathTree.fireTreeExpanded(lastPath);
String name = JOptionPane.showInputDialog("请输入文件夹的名称:", "新建文件夹 ");
if(name!=""){
IconNode newNode = new IconNode(getFolderIcon(),name);
newNode.setAllowsChildren(false);
System.out.println("创建目录:"+getNodePath(node)+name);
if(mkDir(getNodePath(node)+name)){
if(!node.getAllowsChildren())
node.setAllowsChildren(true);
dtm.insertNodeInto(newNode, node, node.getChildCount());
pathTree.setSelectionPath(new TreePath(newNode.getPath()));
pathTree.scrollPathToVisible(new TreePath(newNode.getPath()));
}
else{
JOptionPane.showConfirmDialog(null, "文件夹创建失败!","文件夹创建警告 ",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null);
}
}
}
}
象你这样写逻辑上是有问题的 if (!path.endsWith(File.separator)) {
path = path + File.separator;
if (new File(path).mkdir()) {
System.out.println("创建目录:" + path + "成功!");
return true;
}
}
return false;如果path以"\"结尾,就不会被创建而直接返回false改成下面这样: public boolean mkDir(String path) {
File dir = new File(path);
if (dir.exists()) {
System.out.println("文件夹已经存在!");
return false;
}
if (!path.endsWith(File.separator)) {
path = path + File.separator;
}
if (new File(path).mkdir()) {
System.out.println("创建目录:" + path + "成功!");
return true;
}
return false;
}