Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件变为文件夹_Java_File_Directory - Fatal编程技术网

Java 文件变为文件夹

Java 文件变为文件夹,java,file,directory,Java,File,Directory,所以,我很难处理文件。我以前用过文件,但这一次它们很麻烦 public SaveFile(File newFile) { this.file = newFile; boolean first = false; if(!file.exists()){ file.mkdir(); first = true; } if(file.isDirectory()){ throw new IllegalStateExc

所以,我很难处理文件。我以前用过文件,但这一次它们很麻烦

public SaveFile(File newFile)
{
    this.file = newFile;

    boolean first = false;
    if(!file.exists()){
        file.mkdir();
        first = true;
    }

    if(file.isDirectory()){
        throw new IllegalStateException("Save file can not be a folder");
    }

    if(file.getName().equalsIgnoreCase("current")){
        first = false;
    }

    this.config = YamlConfiguration.loadConfiguration(this.file);

    String name = file.getName();

    name =  name.substring(0, name.lastIndexOf("."));

    if(first){
        config.set("name", name);
        config.set("health", 20.0F);
        config.set("level", 0);
    }

    try {
        config.save(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
当我创建一个保存文件时,它可以正常工作。我可以查看和编辑文件,这是伟大的。但是,如果我尝试创建第二个SaveFile,它会将新文件转换为文件夹,并抛出非法状态异常

这就是它的样子:

public static void main(String[] args){
    SaveFile robert = new SaveFile(new File(SaveFile.getSaveFolder(), "Robert.save"));
    SaveFile james = new SaveFile(new File(SaveFile.getSaveFolder(), "James.save");
}
保存文件robert已创建,看起来与预期的一样。
保存文件james被创建为文件夹,并抛出一个非法状态异常

我认为您的问题在这里,正如我的代码注释中所指出的:

boolean first = false;
if(!file.exists()){
    // the following line is turning the file into a directory if the file
    // doesn't already exist
    file.mkdir();
    first = true;
}

// now you are checking if the directory you just created is a directory
// (this will, of course, be true) and thus throw the exception
if(file.isDirectory()){
    throw new IllegalStateException("Save file can not be a folder");
}

所以看起来“Robert.save”是存在的,因此可以工作,但“James.save”不是这样,所以它被创建为一个目录。

我认为问题就在这里,正如我在代码注释中所指出的:

boolean first = false;
if(!file.exists()){
    // the following line is turning the file into a directory if the file
    // doesn't already exist
    file.mkdir();
    first = true;
}

// now you are checking if the directory you just created is a directory
// (this will, of course, be true) and thus throw the exception
if(file.isDirectory()){
    throw new IllegalStateException("Save file can not be a folder");
}

所以看起来“Robert.save”是存在的,因此可以工作,但“James.save”不是这样,所以它被创建为一个目录。

我认为问题就在这里,正如我在代码注释中所指出的:

boolean first = false;
if(!file.exists()){
    // the following line is turning the file into a directory if the file
    // doesn't already exist
    file.mkdir();
    first = true;
}

// now you are checking if the directory you just created is a directory
// (this will, of course, be true) and thus throw the exception
if(file.isDirectory()){
    throw new IllegalStateException("Save file can not be a folder");
}

所以看起来“Robert.save”是存在的,因此可以工作,但“James.save”不是这样,所以它被创建为一个目录。

我认为问题就在这里,正如我在代码注释中所指出的:

boolean first = false;
if(!file.exists()){
    // the following line is turning the file into a directory if the file
    // doesn't already exist
    file.mkdir();
    first = true;
}

// now you are checking if the directory you just created is a directory
// (this will, of course, be true) and thus throw the exception
if(file.isDirectory()){
    throw new IllegalStateException("Save file can not be a folder");
}

所以它看起来像“Robert.save”存在,并因此起作用,但“James.save”不起作用,所以它被创建为一个目录。

它与这行代码有关:

file.mkdir();
,它从作为参数传递的
文件
对象的路径中生成一个目录。因此,如果在执行之前没有任何文件(
Robert.save
James.save
)存在,它将创建两个名为
/your/path/Robert.save
/your/path/James.save
目录

它只创建一个目录,这可能是因为在执行代码之前,
Robert.save
可能已经存在。因此不会调用
mkDir
方法,因为以下条件未验证:

if(!file.exists())

它与这行代码有关:

file.mkdir();
,它从作为参数传递的
文件
对象的路径中生成一个目录。因此,如果在执行之前没有任何文件(
Robert.save
James.save
)存在,它将创建两个名为
/your/path/Robert.save
/your/path/James.save
目录

它只创建一个目录,这可能是因为在执行代码之前,
Robert.save
可能已经存在。因此不会调用
mkDir
方法,因为以下条件未验证:

if(!file.exists())

它与这行代码有关:

file.mkdir();
,它从作为参数传递的
文件
对象的路径中生成一个目录。因此,如果在执行之前没有任何文件(
Robert.save
James.save
)存在,它将创建两个名为
/your/path/Robert.save
/your/path/James.save
目录

它只创建一个目录,这可能是因为在执行代码之前,
Robert.save
可能已经存在。因此不会调用
mkDir
方法,因为以下条件未验证:

if(!file.exists())

它与这行代码有关:

file.mkdir();
,它从作为参数传递的
文件
对象的路径中生成一个目录。因此,如果在执行之前没有任何文件(
Robert.save
James.save
)存在,它将创建两个名为
/your/path/Robert.save
/your/path/James.save
目录

它只创建一个目录,这可能是因为在执行代码之前,
Robert.save
可能已经存在。因此不会调用
mkDir
方法,因为以下条件未验证:

if(!file.exists())

显然,您正试图在父目录中创建一个文件,您希望检查父目录是否存在,并在需要时首先创建它

您需要获取传递的文件的父级,该文件是您可能希望使用创建的目录。此方法返回必须调用的witch的File对象

您可以这样进行,例如:

public void saveFile(File newFile)
{
    File file = newFile;

    if(! file.exists()){
        File dir = file.getParentFile();
        if(! dir.exists()) {
            if(dir.mkdirs()) {
                System.out.println("parent directory " 
                    + dir.getPath() + " created");
            }
            if(! dir.isDirectory()){
                throw new IllegalStateException("Unable to create directory "
                 + dir.getPath());
            }
        }
    } else {
        if(file.isDirectory()){
            throw new IllegalStateException("Save file can not be a folder");
        }
    }

    // ...

    System.out.println("Save file " + file.getPath() + " created");
}

显然,您正试图在父目录中创建一个文件,您希望检查父目录是否存在,并在需要时首先创建它

您需要获取传递的文件的父级,该文件是您可能希望使用创建的目录。此方法返回必须调用的witch的File对象

您可以这样进行,例如:

public void saveFile(File newFile)
{
    File file = newFile;

    if(! file.exists()){
        File dir = file.getParentFile();
        if(! dir.exists()) {
            if(dir.mkdirs()) {
                System.out.println("parent directory " 
                    + dir.getPath() + " created");
            }
            if(! dir.isDirectory()){
                throw new IllegalStateException("Unable to create directory "
                 + dir.getPath());
            }
        }
    } else {
        if(file.isDirectory()){
            throw new IllegalStateException("Save file can not be a folder");
        }
    }

    // ...

    System.out.println("Save file " + file.getPath() + " created");
}

显然,您正试图在父目录中创建一个文件,您希望检查父目录是否存在,并在需要时首先创建它

您需要获取传递的文件的父级,该文件是您可能希望使用创建的目录。此方法返回必须调用的witch的File对象

您可以这样进行,例如:

public void saveFile(File newFile)
{
    File file = newFile;

    if(! file.exists()){
        File dir = file.getParentFile();
        if(! dir.exists()) {
            if(dir.mkdirs()) {
                System.out.println("parent directory " 
                    + dir.getPath() + " created");
            }
            if(! dir.isDirectory()){
                throw new IllegalStateException("Unable to create directory "
                 + dir.getPath());
            }
        }
    } else {
        if(file.isDirectory()){
            throw new IllegalStateException("Save file can not be a folder");
        }
    }

    // ...

    System.out.println("Save file " + file.getPath() + " created");
}

显然,您正试图在父目录中创建一个文件,您希望检查父目录是否存在,并在需要时首先创建它

您需要获取传递的文件的父级,该文件是您可能希望使用创建的目录。此方法返回必须调用的witch的File对象

您可以这样进行,例如:

public void saveFile(File newFile)
{
    File file = newFile;

    if(! file.exists()){
        File dir = file.getParentFile();
        if(! dir.exists()) {
            if(dir.mkdirs()) {
                System.out.println("parent directory " 
                    + dir.getPath() + " created");
            }
            if(! dir.isDirectory()){
                throw new IllegalStateException("Unable to create directory "
                 + dir.getPath());
            }
        }
    } else {
        if(file.isDirectory()){
            throw new IllegalStateException("Save file can not be a folder");
        }
    }

    // ...

    System.out.println("Save file " + file.getPath() + " created");
}

您希望看到什么
file.mkdir()可以吗?我希望它会生成文件,它会这样做。使用
file.mkdirs()
抛出FileNotFoundExceptionNope,创建一个目录。在使用方法之前,请检查javadoc中的方法,以了解如何使用它们。javadoc中唯一的区别是这一行
包括任何必要但不存在的父目录。
因此,在这两行上,它都说,
创建了以这个抽象路径名命名的目录。
那么,我怎么知道呢?这没有什么帮助。您希望看到什么
file.mkdir()