如果目录不存在,则创建一个目录,然后在该目录中创建文件 [英] Create a directory if it does not exist and then create the files in that directory as well

查看:247
本文介绍了如果目录不存在,则创建一个目录,然后在该目录中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

条件是如果目录存在,它必须在该特定目录中创建文件而不创建新目录。

Condition is if directory exists it has to create files in that specific directory with out creating a new directory.

以下代码仅创建具有新目录的文件但是不适用于现有目录。例如,目录名称将类似于GETDIRECTION

The below code only creating a file with new directory but not for 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();


推荐答案

此代码首先检查目录是否存在如果没有,则创建它,然后创建文件。请注意,我无法验证您的某些方法调用,因为我没有完整的代码,因此我假设调用了诸如 getTimeStamp()之类的内容 getClassName()将起作用。您还应该对使用任何 java.io。* 类时可能引发的 IOException 执行某些操作 - 写入文件的函数应抛出此异常(并在其他地方处理),或者您应该直接在方法中执行此操作。另外,我假设 id 的类型为 String - 我不知道你的代码没有明确定义它。如果它是像 int 那样的东西,你可能应该把它转换为 String ,然后在fileName中使用它我已经在这里完成了。

This code checks for the existence of the directory first and creates it if not, and creates the file afterwards. Please note that I couldn't verify some of your method calls as I don't have your complete code, so I'm assuming the calls to things like getTimeStamp() and getClassName() will work. You should also do something with the possible IOException that can be thrown when using any of the java.io.* classes - either your function that writes the files should throw this exception (and it be handled elsewhere), or you should do it in the method directly. Also, I assumed that id is of type String - I don't know as your code doesn't explicitly define it. If it is something else like an int, you should probably cast it to a String before using it in the fileName as I have done here.

另外,我用 concat替换你的追加电话 +

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);
    }
}

你可能不应该使用像这样的裸路径名称你想在Microsoft Windows上运行代码 - 我不确定它将如何处理文件名中的 / 。为了完全可移植性,您应该使用 File.separator 之类的内容来构建路径。

You should probably not use bare path names like this if you want to run the code on Microsoft Windows - I'm not sure what it will do with the / in the filenames. For full portability, you should probably use something like File.separator to construct your paths.

编辑:根据 JosefScript的评论下面,没有必要测试目录是否存在。 directory.mkdir()调用将返回 true 如果它创建了一个目录,并且 false 如果没有,包括目录已经存在的情况。

Edit: According to a comment by JosefScript below, it's not necessary to test for directory existence. The directory.mkdir() call will return true if it created a directory, and false if it didn't, including the case when the directory already existed.

这篇关于如果目录不存在,则创建一个目录,然后在该目录中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆