1.在JAVA中新建文件夹如果我直接写publicclassTest{publicstaticvoidmain(String[]args){Filef=newFile("D:/temp/1.txt");}运行后并不会在要求的目录生成文件,而我补充语句PrintWrit...
1.
在JAVA中新建文件夹
如果我直接写
public class Test{
public static void main(String[]args){
File f = new File("D:/temp/1.txt");
}
运行后并不会在要求的目录生成文件,而我补充语句
PrintWriter out = new PrintWriter(file);(main方法抛出异常)
out.print(" ");
out.close();
却又可以。
我个人认为我没加这三句,就出现所谓的"file has never been read",所以才不能在指定目录建立一个文件。请问是这样吗?(请解释)
2.
新建文件夹:(根据一个JTree的结点,变量名为newNode)
String url = getURL(path); //返回的url的末尾为".txt"
File file;
if(newNode.getnextSibling()!=null && newNode.getnextSibling().getChildCount()>=1){
String newURL = url.substring(0, url.length()-4);
file = new File(newURL);
file.mkdir();
}
else{
file = new File(url);
}
PrintWriter out = null;
try {
out = new PrintWriter(file);
}catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out.print(" ");
out.close();
file.deleteOnExit();
}
运行后,如果我要添加一个文件夹,会出现诸如此类的异常:
java.io.FileNotFoundException: D:\CPU Simulator\Disk\temp\temp.txt (系统找不到指定的路径。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(FileOutputStream.java:179)
at java.io.FileOutputStream.(FileOutputStream.java:131)
at java.io.PrintWriter.(PrintWriter.java:218)
at control.TreeEditor.editTree(TreeEditor.java:72)
at modeling.TreeListener.actionPerformed(TreeListener.java:24)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
请高手赐教,答案如果满意(最好带点注释),另外再加15分(我现在全部家当才35分)。出去给出的10分,还有25分。呵呵,拜托大家了!
if(newNode.getnextSibling()!=null && newNode.getnextSibling().getChildCount()>=1){
String newURL = url.substring(0, url.length()-4);
file = new File(newURL);
file.mkdir();
}
对不起大家,我可能问题没有说清楚。我要创建一个新文件或文件夹时,必须根据兄弟结点的属性,如果兄弟结点是文件夹,则它也必须为文件夹,否则为文件。
展开