如何检查特定字符之前是否有字符串

时间:2017-08-28 16:07:05

标签: java

生病尽量保持尽可能短:) 所以我有这个代码,它的功能是将文件放在用户指定的目录中。该代码的部分内容包括:

  1. 找出用户甚至想要首先制作文件
  2. 找出文件名(扫描程序,确保文件名有扩展名)
  3. 找出用户指定的目录(扫描程序,检查用户输入的目录是否有效,检查目录是否存在以及是否存在,创建新目录)
  4. 制作文件(鉴于该文件尚未存在)
  5. 我的问题在1和2中。

    问题1:用户可以输入类似" .txt"并且在实际制作文件时,您需要输入实际名称。 (我做了一些检查文件是否有扩展名的东西。也许类似的东西?)

    2中的问题:开始部分的while循环没有退出......为什么......

    CODE:

    package filecreator;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.*;
    
    public class FileCreator {
    
    public static Scanner sc = new Scanner(System.in);
    public static boolean success = false;
    public static String filename;
    public static String filedir;
    public static String fileexten;
    public static String filedir2;
    public static File file;
    public static File dir;
    
    public static void main(String[] args) throws IOException {
    
        System.out.println("********************************");
        System.out.println("* Welcome to File Creator 2.0! *");
        System.out.println("********************************");
        System.out.println(" ");
    
        //Starting prompt
        while(!success) {
    
            System.out.println("Would you like to create a file? Y/N?");
            String usrans = sc.nextLine();
    
            if(usrans.equalsIgnoreCase("y")) {
    
                System.out.println("Proceeding with file creation...");
                System.out.println(" ");
                break;
    
            } else if(usrans.equalsIgnoreCase("n")) {
    
                System.out.println("Exiting Program...");
                System.exit(0);
    
            } else if(!usrans.equalsIgnoreCase("y") || !usrans.equalsIgnoreCase("n")) {
    
                System.out.println("That is not a valid answer! Please try again!");
                System.out.println(" ");
            }
        }
    
        System.out.println("Please enter a valid file name:");
    
        //choosing name of file
        while(!success) {
    
           filename = sc.nextLine();
           fileexten = filename.substring(filename.lastIndexOf(".") + 1);
    
            if(filename.isEmpty()) {
    
                System.out.println("Please enter a file name!");
    
            } else if(filename.contains("/") || filename.contains(":") || 
                      filename.contains("*") || filename.contains("?") ||
                      filename.contains("<") || filename.contains(">") ||
                      filename.contains("|") || filename.contains("\"") ||
                      filename.contains("\\")) {
    
                System.out.println("Please do not include / \\ : * ? \" < > |");
    
            } else if(!filename.contains(".")) {
    
                System.out.println(" ");
                System.out.println("Please specify a valid file extension!");
    
            } else if (fileexten.isEmpty() || fileexten.contains("[^\\p{IsDigit}\\p{IsAlphabetic}]")) {
    
                System.out.println(" ");
                System.out.println("Please specify a valid file extension!");
    
            } else {
    
                System.out.println(" ");
                System.out.println("File name \"" + filename + "\" chosen");
                break;
            }
        }
    
        System.out.println(" ");
        System.out.println("Where would you like to have your file saved?");
        System.out.println("Please enter a valid directory");
    
        //finding directory if directory exists and creating one if it dosent
        while(!success) {
    
            filedir = sc.nextLine();
            dir = new File(filedir);
    
            do {
    
                System.out.println(""
                        + "That is not a valid directory");
                filedir = sc.nextLine();
    
            } while((!filedir.contains(":") && !filedir.contains("\\")));
    
            System.out.println("Checking to see if directory exists...");
    
            if(!dir.exists()) {
    
                System.out.println("Directory does not exist! Would you like to create one? Y/N?");
                String usrans = sc.nextLine();
    
                if(usrans.equalsIgnoreCase("y")) {
    
                    dir = new File(filedir);
                    success = dir.createNewFile();
    
                    if(!success) {
    
                        System.out.println("Created!");
                        break;
    
                    } else {
    
                        System.out.println("Something went wrong! Aborting Program...");
                        System.exit(0);
                    } 
    
                } else if(usrans.equalsIgnoreCase("n")) {
    
                    System.out.println("Directory not created! Aborting program...");
                    System.exit(0);
    
                } else if(!usrans.equalsIgnoreCase("y") || !usrans.equalsIgnoreCase("n")) {
    
                    System.out.println("That is not a valid answer!");
                }
            }
        }
    
        System.out.println(" ");
        System.out.println("Looking to see if \"" + filename + "\" already exists...");
    
        file = new File(filedir,filename);
    
        if(file.exists()) {
    
            System.out.println(" ");
            System.out.println("\"" + filename + "\"" + " already exists! Closing Program...");
            System.exit(0);
    
        } else {
    
            System.out.println(" ");
            System.out.println("\"" + filename + "\"" + " doesn't exists! Creating now...");
            file.createNewFile();
            success = file.createNewFile();
    
            if(!success) {
    
                System.out.println(" ");
                System.out.println("\"" + filename + "\"" + " successfully created!");
    
            } else {
    
                System.out.println(" ");
                System.out.println("Error! File not created");
            } 
        }
    }
    

    }

2 个答案:

答案 0 :(得分:2)

解决方案1:使用以下命令检查文件名的有效性,而不是那些长条件子句。

if ( filename.toLowerCase().matches( "[a-z0-9_-][a-z0-9_.-]*\\.[a-z0-9]+" ) )

解决方案2:因为您的程序希望用户输入 n 退出,而不是单独完成。

答案 1 :(得分:1)

关于第一个问题,您可以创建regex to validate the filename

boolean validFilename = filename.matches("[_a-zA-Z0-9\\-\\.]+"); //regex to validate a filename

if(!validFilename){
   //output error to user and ask to try again
}

对于您的第二个问题,您将success变量设为false,并且您没有更改其值。那么你的条件while(!success)将始终返回true,你的循环将&#34;永远&#34;