我想出了以下代码...我的文档没有创建。 我没有收到任何错误消息。关于Java API,我按正确的顺序做了一切。该目录是以正确的方式创建的...所以我不必担心这一点。
任何人都有想法或暗示在哪里看?
public static void main(String[] args) throws Exception{
String path = null;
String destination = "/myfolder/test/" + createRandomPath(path);
try {
boolean status;
status = new File(destination).mkdir();
} catch (Exception e) {
System.out.println("Fehler: " + e.getMessage());
}
File document = new File(destination + "temp.docx");
//Edit: Here is the solution..Thank you
document.createNewFile();
}
static String createRandomPath(String path){
UUID uuid = UUID.randomUUID();
path = uuid.toString().replace('-', 'A').substring(0,9);
System.out.println(path);
return path;
}
答案 0 :(得分:4)
你需要调用createNewFile()来错误地在磁盘上创建一个新文件,如果这就是你所要求的。
所以你需要以下形式的代码:
File f = new File("C:\\temp\\newfile.txt");
//new file does not exist on disk yet
f.createNewFile();
//f should be created on disk now
答案 1 :(得分:1)
File Document...
行在内存中创建File
类的实例。正如James B所说,你需要创建文件,也许是这样的:
File doc = new File(destination + "temp.docx");
doc.createNewFile();
(请记住将其包裹在try... catch
中,因为您必须陷阱IOException
)
答案 2 :(得分:0)
我已经修改并执行了我,我修改了路径并执行了 “”c:\ myfolder \ test“+ createRandomPath(path);” 它有效。
public static void main(String[] args) throws Exception{
String path = null;
String destination = "c:\\myfolder\\test" + createRandomPath(path);
try {
boolean status;
status = new File(destination).mkdir();
} catch (Exception e) {
System.out.println("Fehler: " + e.getMessage());
}
File document = new File(destination + "temp.doc");
//Edit: Here is the solution..Thank you
document.createNewFile();
}
static String createRandomPath(String path){
UUID uuid = UUID.randomUUID();
path = uuid.toString().replace('-', 'A').substring(0,9);
System.out.println(path);
return path;
}
答案 3 :(得分:0)
运行以下代码。它会起作用
public class Test1{
public static void main(String[] args) throws Exception
{
String path = null;
String destination = "/myfolder/test/" + createRandomPath(path);
try
{
boolean status;
status = new File(destination).mkdirs();
} catch (Exception e)
{
System.out.println("Fehler: " + e.getMessage());
}
File document = new File(destination + "/temp.docx");
//Edit: Here is the solution..Thank you
document.createNewFile();
}
static String createRandomPath(String path)
{
UUID uuid = UUID.randomUUID();
path = uuid.toString().replace('-', 'A').substring(0, 9);
System.out.println(path);
return path;
}
}
当我在netbeans上运行上面的代码时,它在“C:\ myfolder \ test \ b75ba657A”中创建了文件“temp.docx”