我正在創建一個目錄,然後是一個文件。問題是該目錄已創建,但文件不是。爲什麼不創建文件
當我這樣做,我創建和它打印出的是「成功創建新的文件:?和文件名
誰能幫助理解爲什麼
public class Main
{
public static void main(String[] args) throws IOException
{
Scanner reader = new Scanner (System.in);
boolean success = false;
GetFiles getFile = new GetFiles();
System.out.println("Enter path of directory to create");
String dir = reader.nextLine();
// Create a new directory in Java, if it doesn't exists
File directory = new File(dir);
if(directory.exists())
{
System.out.println("Directory already exists");
}
else
{
System.out.println("Directory not exists, creating now");
success = directory.mkdir();
if(success)
System.out.println("Successfuly created new directory");
else
System.out.println("Failed to create new directory");
}
// Creatning new file in Java, only if not exists
System.out.println("Enter file name to be created");
String filename = reader.nextLine();
File f = new File(filename);
if(f.exists())
{
System.out.println("File already exists");
}
else
{
System.out.println("No such file exists, creating now");
success = f.createNewFile();
if(success)
System.out.printf("Successfully created new file: : %s%n", f);
else
System.out.printf("Failed to create new file: %s%n", f);
}
reader.close();
getFile.getAllFiles(directory);
}
}
你並沒有創建'File' _inside_目錄 - 你正在創建它_next到目錄。 – 2014-10-05 13:58:32
由於未定義GetFiles類,因此您的示例未編譯。 – EJK 2014-10-05 13:59:06
你可能沒有看到正確的地方。而不是打印'f',打印'f.getAbsolutePath()'。 – 2014-10-05 14:00:51