zoukankan      html  css  js  c++  java
  • How to Create a new file in java

    There are various ways in which you can create a new file in Java. In this article, I’ve outlined the two most recommended way of creating new files.

    You can use [Files.createFile(path)](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createFile(java.nio.file.Path, java.nio.file.attribute.FileAttribute...)) method to create a new File in Java:

    package com.callicoder;
    
    import java.io.IOException;
    import java.nio.file.FileAlreadyExistsException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class CreateNewFile {
    
        public static void main(String[] args)  {
            // New file path
            Path filePath = Paths.get("./bar.txt");
    
            try {
                // Create a file at the specified file path
                Files.createFile(filePath);
                System.out.println("File created successfully!");
    
            } catch (FileAlreadyExistsException e) {
                System.out.println("File already exists");
            } catch (IOException e) {
                System.out.println("An I/O error occurred: " + e.getMessage());
            } catch (SecurityException e) {
                System.out.println("No permission to create file: " + e.getMessage());
            }
        }
    }
    

    Create New File with missing parent directories using Java NIO

    There are scenarios in which you may want to create any missing parent directories while creating a File. You can use [Files.createDirectories(path)](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createDirectories(java.nio.file.Path, java.nio.file.attribute.FileAttribute...)) function to create missing parent directories before creating the file.

    package com.callicoder;
    
    import java.io.IOException;
    import java.nio.file.FileAlreadyExistsException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class CreateNewFile {
    
        public static void main(String[] args)  {
            // New file path
            Path filePath = Paths.get("java/io/bar.txt");
    
            try {
                // Create missing parent directories
                if(filePath.getParent() != null) {
                    Files.createDirectories(filePath.getParent());
                }
    
                // Create a file at the specified file path
                Files.createFile(filePath);
                System.out.println("File created successfully!");
    
            } catch (FileAlreadyExistsException e) {
                System.out.println("File already exists");
            } catch (IOException e) {
                System.out.println("An I/O error occurred: " + e.getMessage());
            } catch (SecurityException e) {
                System.out.println("No permission to create file: " + e.getMessage());
            }
        }
    }
    

    Create New File in Java using java.io.File class - JDK 6+

    You can also use the File.createNewFile() method to create a new File in Java. It returns a boolean value which is -

    • true, if the file does not exist and was created successfully
    • false, if the file already exists
    package com.callicoder;
    
    import java.io.File;
    import java.io.IOException;
    
    public class CreateNewFile {
    
        public static void main(String[] args) {
            // Instantiate a File object with a file path
            File file = new File("./foo.txt");
    
            try {
                // Create the file in the filesystem
                boolean success = file.createNewFile();
    
                if (success) {
                    System.out.println("File created successfully!");
                } else {
                    System.out.println("File already exists!");
                }
            } catch (IOException e) {
                System.out.println("An I/O error occurred: " + e.getMessage());
            } catch (SecurityException e) {
                System.out.println("No sufficient permission to create file: " + e.getMessage());
            }
        }
    }
    

    Create New File along with missing parent directories with java.io.File class

    If you want to create missing parent directories while creating a file, then you can explicitly create the directories by calling file.getParentFile().mkdirs() method:

    package com.callicoder;
    
    import java.io.File;
    import java.io.IOException;
    
    public class CreateNewFile {
    
        public static void main(String[] args) {
            // Instantiate a File object with a file path
            File file = new File("java/io/foo.txt");
    
            try {
                // Create missing parent directories
                if(file.getParentFile() != null) {
                    file.getParentFile().mkdirs();
                }
    
                // Create the file
                boolean success = file.createNewFile();
    
                if (success) {
                    System.out.println("File created successfully!");
                } else {
                    System.out.println("File already exists!");
                }
            } catch (IOException e) {
                System.out.println("An I/O error occurred: " + e.getMessage());
            } catch (SecurityException e) {
                System.out.println("No sufficient permission to create file: " + e.getMessage());
            }
        }
    }
    
  • 相关阅读:
    供安全工程师实用的SOC模型
    Windows-NTFS-ADS在渗透测试中的利用
    域安全的基础知识上
    Linux下利用动态链接劫持库函数并注入代码
    多线程sshd爆破程序代码
    Windows NTLM Hash和Hash传递、Key传递攻击
    替换linux系统文件etc下passwd文件的字段获取真正的root权限
    攻防对抗中常用的windows命令(渗透测试和应急响应)
    获取权限后权限维持的方法
    RocketMQ启动mqbroker.cmd没反应
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/13094283.html
Copyright © 2011-2022 走看看