Java创建、删除文件-同时支持Linux目录或Windows目录

文件分割符:File.separatorChar

public static void fileTest(){
        String filePath = System.getProperty("user.dir") + File.separatorChar + "photo" + File.separatorChar;
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(filePath + fileName);
        System.out.println("创建目录:" + filePath);
        File  dir=new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        boolean newFile = false;
        try {
            // 创建文件
            newFile = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("是否创建成功:" + newFile);
        System.out.println("文件目录:" + file.getParent());
        System.out.println("文件路径:" + file.getPath());
        System.out.println("创建文件:" + file.getName());
        if (file.exists()) {
            boolean delete = file.delete();
            System.out.println("是否删除成功:" + delete);
        }
    }

猜你喜欢

转载自blog.csdn.net/Angel_asp/article/details/128975995
今日推荐