Java – create a directory, if it does not exist, and then create the files in that directory

The condition is that if a directory exists, you must create files in that specific directory and create a new directory

The following code only creates a file with a new directory but not an existing directory For example, the directory name would be like "getdirection"

String PATH = "/remote/dir/server/";

String fileName = PATH.append(id).concat(getTimeStamp()).append(".txt");  

String directoryName = PATH.append(this.getClassName());   

File file  = new File(String.valueOf(fileName));

File directory = new File(String.valueOf(directoryName));

 if(!directory.exists()){

             directory.mkdir();
            if(!file.exists() && !checkEnoughDiskSpace()){
                file.getParentFile().mkdir();
                file.createNewFile();
            }
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(value);
bw.close();

Solution

This code first checks the existence of the directory, if not, creates it, and then creates the file Note that I can't verify some method calls because I don't have complete code, so I assume that calling gettimestamp () and getclassname () works You should also be using any Java io. * Class - the function you write to the file should throw this exception (and handle it elsewhere), or you should directly in the method In addition, I think id is a string type - I didn't know your code didn't explicitly define it If it is something like int, you should convert it to a string before using it in the file name, as I did here

In addition, I replaced your append phone with concat or as I think appropriate

public void writeFile(String value){
    String PATH = "/remote/dir/server/";
    String directoryName = PATH.concat(this.getClassName());
    String fileName = id + getTimeStamp() + ".txt";

    File directory = new File(directoryName);
    if (! directory.exists()){
        directory.mkdir();
        // If you require it to make the entire directory path including parents,// use directory.mkdirs(); here instead.
    }

    File file = new File(directoryName + "/" + fileName);
    try{
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(value);
        bw.close();
    }
    catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}

If you want to run code on Microsoft Windows, you should not use a bare path name like this - I don't know how it will be handled / in a file name For complete portability, you should use something like file Separator to build the path

Edit: according to the comments of josefscript below, it is not necessary to test the existence of the directory If you create a directory, directory The mkdir() call will return true and false, including the case where the directory already exists

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>