创建目录而不是文件

时间:2014-01-31 12:20:57

标签: java android file

我想创建新文件,但它会创建目录...我尝试了很多检查和方法..在我的应用程序的不同部分创建新文件工作正常...

public void copyDatabaseFromAssets() throws IOException {
            InputStream inputStream = app.getAssets().open(Static.DATABASE_FILE_NAME);
            File directory = new File(getInternalDatabaseFolder());
            boolean exist = directory.exists();
            boolean isDirecotry = directory.isDirectory();
            boolean isFile = directory.isFile();
            if(!directory.exists())directory.mkdirs();
            File targetFile = new File(directory.getAbsolutePath(),Static.DATABASE_FILE_NAME);
            boolean exist2 = targetFile.exists();
            boolean isDirecotry2 = targetFile.isDirectory();
            boolean isFile2 = targetFile.isFile();
            boolean created = targetFile.createNewFile();
            FileTool.copyDataBaseFromAssets(inputStream,targetFile);
        }

调试 - >

directory = /data/data/***/databases   
exist = true    
isDirectory = true   
isFile = false

targetFile = /data/data/***/databases/database.sqlite   
exist2 = true    
isDirectory2 = true
isFile2 = false    
created = false

错误 - >

java.io.FileNotFoundException: /data/data/***/databases/database.sqlite: open failed: EISDIR (Is a directory)

1 个答案:

答案 0 :(得分:1)

exist2表示它已存在且isDirectory2表示它是一个文件夹。尝试删除database.sqlite并再次运行相同的代码