[Java][File]使用mkdir以及CreateNewFile来制作游戏存档的分级目录+异常抛出机制

2023-12-21 14:05:34
文件夹分类:
Resource\\Image
Resource\\Voice
Resource\\Video

Information\\Characters
Information\\mobs
Information\\Backpacks

Plugin\\Serves
Plugin\\Consumers

文件的分类:
Information\\Characters\\Male.txt
Information\\Characters\\Female.txt
Information\\mobs\\Wolf.txt
Information\\mobs\\Zombie.txt
Information\\Backpacks\\Player1.txt
Information\\Backpacks\\Player2.txt
Information\\Backpacks\\player3.txt

其实我觉着这样写挺笨的,一种优化的方向是写一个嵌套的for for循环来创建文件夹 比如f[i][k].mkdirs 这样的效率会更高

import java.io.File;
import java.io.IOException;

public class Main{
    public static void main(String[] args){
        //完成游戏本地文件夹以及文件的创建

        /*
           文件夹分类:
           Resource\\Image
           Resource\\Voice
           Resource\\Video

           Information\\Characters
           Information\\mobs
           Information\\Backpacks

           Plugin\\Serves
           Plugin\\Consumers

           文件的分类:
           Information\\Characters\\Male.txt
           Information\\Characters\\Female.txt
           Information\\mobs\\Wolf.txt
           Information\\mobs\\Zombie.txt
           Information\\Backpacks\\Player1.txt
           Information\\Backpacks\\Player2.txt
           Information\\Backpacks\\player3.txt
         */

        //1.文件夹的创建
        File f00 = new File("F:\\test\\Resource\\Image");
        File f01 = new File("F:\\test\\Resource\\Voice");
        File f02 = new File("F:\\test\\Resource\\Video");
        File f10 = new File("F:\\test\\Information\\Characters");
        File f11 = new File("F:\\test\\Information\\mobs");
        File f12 = new File("F:\\test\\Information\\Backpacks");
        File f20 = new File("F:\\test\\Plugin\\Serves");
        File f21 = new File("F:\\test\\Plugin\\Consumers");
        f00.mkdirs();
        f01.mkdirs();
        f02.mkdirs();
        f10.mkdirs();
        f11.mkdirs();
        f12.mkdirs();
        f20.mkdirs();
        f21.mkdirs();

        File maleFile = new File("F:\\test\\Information\\Characters\\Male.txt");
        File femaleFile = new File("F:\\test\\Information\\Characters\\Female.txt");
        File wolfFile = new File("F:\\test\\Information\\mobs\\Wolf.txt");
        File zombieFile = new File("F:\\test\\Information\\mobs\\Zombie.txt");
        File player1File = new File("F:\\test\\Information\\Backpacks\\Player1.txt");
        File player2File = new File("F:\\test\\Information\\Backpacks\\Player2.txt");
        File player3File = new File("F:\\test\\Information\\Backpacks\\player3.txt");

        try {
            maleFile.createNewFile();
            System.out.println("Created file: " + maleFile.getAbsolutePath());

            femaleFile.createNewFile();
            System.out.println("Created file: " + femaleFile.getAbsolutePath());

            wolfFile.createNewFile();
            System.out.println("Created file: " + wolfFile.getAbsolutePath());

            zombieFile.createNewFile();
            System.out.println("Created file: " + zombieFile.getAbsolutePath());

            player1File.createNewFile();
            System.out.println("Created file: " + player1File.getAbsolutePath());

            player2File.createNewFile();
            System.out.println("Created file: " + player2File.getAbsolutePath());

            player3File.createNewFile();
            System.out.println("Created file: " + player3File.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}

文章来源:https://blog.csdn.net/qq_37511412/article/details/135128189
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。