如何使用Java在除C驱动器之外的任何可用驱动器中创建文件夹

时间:2019-06-18 06:08:19

标签: java

我需要使用java和除c驱动器之外的任何驱动器创建一个文件夹。 如果有三个系统具有不同的驱动器,那么我想为所有这些系统使用相同的代码,在除Java中使用Java代码的Windows中的c驱动器之外的任何可用驱动器中创建文件夹

1 个答案:

答案 0 :(得分:0)

根据驱动器号创建文件夹的代码:

  public class makeFolder {
    public static void main(String[] args) {
        String location = "D:/abc";
        // split the location value
        String[] values = location.split(":");
        // check here the drive letter.
        if(!values[0].equalsIgnoreCase("C") && !values[0].equals("")) {
            Path path = Paths.get(location);
            if (!Files.exists(path)) {
                try {
                    Files.createDirectories(path);
                    System.out.println("Success");
                } catch (IOException e) {
                    //fail to create directory
                    e.printStackTrace();
                }
            }
        }


    }

}

希望

这有帮助:)