如何检查用户输入的字符串是否包含文件扩展名

时间:2017-08-24 02:28:25

标签: java string file

因此,在此程序中,用户可以创建/指定目录并创建文件,因为该文件尚不存在。当用户输入文件名时,他们也需要输入文件扩展名。我遇到的问题是如何使代码在用户输入的字符串末尾查找文件扩展名。我竟然检查了一下“。”但我被困在程序可以看到文件是否有.json或.txt或“。”之后的任何内容的方式。

TL; DR 如何在用户输入的字符串末尾检查文件扩展名(< - not a word)

请注意,以下代码尚未完成,我遇到的情况内有评论。

package filecreator.coolversion;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class FileCreatorCoolversion {

public static Scanner sc = new Scanner(System.in);
public static boolean success = false;
public static String filename;
public static String filedir;
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(" ");

    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...");
            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(" ");
        }
    }

    while(!success) {

        System.out.println(" ");
        System.out.println("Please enter a valid filename:");
        filename = sc.nextLine();

        if(filename.isEmpty()) {

            System.out.println("Please enter a file name!");
            break;

        } 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("Please add a apropriate file extensions");

        } else if (filename.) {

            //HERE IS WHERE IM STUCK

        } 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");

    while(!success) {

        filedir = sc.nextLine();

        if(!filename.contains(":")) {

            System.out.println(" ");
            System.out.println("Please enter a valid directory!");

        } else if(!filename.contains("\\")) {

            System.out.println(" ");
            System.out.println("Please enter a valid directory!");

        } else {

            System.out.println("File directory \"" + filedir + "\" chosen");
            break;
        }
    }

    System.out.println(" ");
    System.out.println("Creating file...");


}

}

3 个答案:

答案 0 :(得分:1)

您可能想要做的就是检查您在之前条件中检查的.的lastIndex,然后查看之后是否有任何字符串:

String extension = filename.substring(filename.lastIndexOf(".") + 1);
//Checks if there is any extension after the last . in your input
if (!extension.isEmpty()) {
    System.out.print("This is the extension - " + extension);
}

答案 1 :(得分:1)

查找文件扩展名(如果有):

static String getExtension(String str){
    int begin = str.lastIndexOf(".");
    if(begin == -1)
        return null;
    String extension = str.substring(begin + 1);
    return extension;
}   

检查扩展名是否有效:

static boolean isExtensionValid(String extension){
    String validExtensions [] = new String[]{
        "json", "txt"
    };
    for(String str : validExtensions )
        if(str.equals(extension))
            return true;

    return false;
}

测试整个事情:

public static void main (String[] args){
    String val = "Kappa.txt";
    String extension = getExtension(val);

    if(extension != null){
        System.out.println(extension);
        System.out.println(isExtensionValid(extension));
    } else {
    //Handle no extension found here
    }
}

答案 2 :(得分:1)

如果你想在.之后获得任何扩展名,你可以在这里使用String split一些例子

String exts[] = filename.split("\\.");
int extIndex = exts.length;\\find array length incase user inputed more than one dot

System.out.println(exts[extIndex - 1]); \\ here you get the extension