2016-12-04 26 views
0

我创建了一个程序,该程序可以创建一个目录,然后在该目录中放置一个文件。正如你所看到的,在这个人指定目录之后,程序应该检查目录是否存在,如果目录不存在或创建一个目录,或者说已经有一个具有该目录的目录。每当我运行该程序,并且我放入一个已经存在的目录时,而不是说该目录已经存在,它会直接跳到失败消息,并继续在现有目录中创建新文件。我该如何修复代码,以便它能提供正确的“文件存在”消息,然后继续执行代码,而不是仅仅说出失败消息?我的程序不执行if语句中的代码

Please enter where you want your file... 
/Users/FyuZheN/Desktop/test 
[Console] Directory does not exist! Creating one... 
[Console] Fail! Couldn't create directory! 
[Console] What would you like to call your new file? 
test.jar 
[Console] Created new file test.jar 
Successfully created new file: /Users/FyuZheN/Desktop/test/test.jar 

此外,如果用户指定的目录是不能存在的目录。这种情况发生......

Please enter where you want your file... 
awdawdawd 
[Console] Directory does not exist! Creating one... 
[Console] Success! Created directory awdawdawd 
[Console] What would you like to call your new file? 
dawdawd 
[Console] Created new file dawdawd 
Successfully created new file: awdawdawd/dawdawd 

我怎样才能重新编码,以便它给出了正确的错误消息,并再次要求用户一个有效的目录?

代码:

import java.awt.*; 
import java.io.*; 
import java.util.*; 

public class findFile { 

public static void main (String[] args) throws IOException { 

    Scanner s = new Scanner(System.in); 
    boolean success = false; 

    System.out.println("Please enter where you want your file..."); 
    String usrdir = s.nextLine(); 

    File directory = new File(usrdir); 

    if (directory.exists() && directory.isFile()) { 

     System.out.println("[Console] This directory already exists"); 

    } else { 

     System.out.println("[Console] Directory does not exist! Creating one..."); 
     success = directory.mkdir(); 

     if (success) { 

      System.out.println("[Console] Success! Created directory " + usrdir); 

     } else { 

      System.out.println("[Console] Fail! Couldn't create directory!"); 
     } 
    } 

    System.out.println("[Console] What would you like to call your new file?"); 
    String filename = s.next(filename = ".txt"); 

    File f = new File(directory, filename); 

    if(f.exists() && f.isFile()) { 

     System.out.println("[Console] File already exists!"); 

    } else { 

     System.out.println("[Console] Created new file " + filename); 
     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); 
     } 
    } 

    s.close(); 
} 
} 
+0

添加一个while循环和布尔folderexists + if(folderexists){添加新文件}。 –

+0

'isFile()'对于一个目录来说总是为false。也许你打算调用[isDirectory()](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#isDirectory--)? – VGR

+0

directory.exists()和directory.isFile()都必须返回true。因为它从未进入该区域,这意味着其中一个或两个都不能恢复正常。也许VGR或者''&&'应该是'||'?我对这里的单词文件的使用并不熟悉,因为如果目录包含一个文件,您可以跳过并且不要求文件? –

回答

0

要使代码再问,做某事喜欢:

while(true){ 
//ask for new foldername 
if(usrdir.equals("exit")){ 
    break;//the user should be able to.leave your program 
} 
//rest of your code 
} 

要创建一个新的文件,你需要一个文件夹,这样做:

boolean folderexists=false; 
//if folder creation worked or if folder exists set to true 
if(folderexists){ 
//ask for filename etc 
} 
+0

我不完全确定你来自哪里。你介意更详细地解释你的答案吗? –