java - 无法创建和复制文件

标签 java android exception android-asynctask

我的程序应该按如下方式工作:

1.Copy the new database in the program folder
2.Import the records from the old database to the new one.

但由于某种原因我得到了一个异常(exception)。为什么?

protected Object doInBackground(Object[] objects) {

        String LOCAL_DATABASE_PATH = getApplicationInfo().dataDir + File.separator +
                "databases" + File.separator;


        File fileDir = new File(LOCAL_DATABASE_PATH);
        if (!fileDir.exists())
            fileDir.mkdirs();

        File tempFile = new File(LOCAL_DATABASE_PATH + DATABASE_NAME);

        try {
            tempFile.createNewFile(); // here I catch exception

            InputStream is = SplashActivity.this.getAssets().open(
                    DATABASE_NAME);
            FileOutputStream os = new FileOutputStream(new File(
                    LOCAL_DATABASE_PATH, DATABASE_NAME));

            int bufferLength = 0;
            byte[] buffer = new byte[2048];

            while ((bufferLength = is.read(buffer)) > 0) {
                os.write(buffer, 0, bufferLength);
            }

            Preferences.getInstance(SplashActivity.this).
                    set(Preferences.IS_DATABASE_COPYING_ON_DEVICE, true);

            is.close();
            os.close();



        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

我收到以下错误

java.io.IOException: open failed: EACCES (Permission denied)
08-28 09:32:27.977  32558-32558/com.DriverNotes.AndroidMobileClientTest D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.io.File.createNewFile(File.java:948)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at com.DriverNotes.AndroidMobileClientTest.SplashActivity$DataBaseLoadTask.doInBackground(SplashActivity.java:73)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-28 09:32:27.977  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.io.File.createNewFile(File.java:941)
08-28 09:32:27.987  32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ ... 7 more

最佳答案

在创建 tempFile 时尝试使用您已经创建的 fileDir 以确保它存在。

File tempFile = new File(fileDir, DATABASE_NAME);

在你创建一个新文件之前,你应该通过调用它的 exits() 方法来检查它是否已经存在。然后,与其再次打开它,不如继续使用它。或者至少在再次打开之前将其关闭。

if(!tempFile.exists())
   tempFile.createNewFile();

InputStream is = SplashActivity.this.getAssets().open(
                 DATABASE_NAME);
FileOutputStream os = new FileOutputStream(tempFile);

使用 tempFile 创建您的 FileOutPutStream 或关闭 tempFile 以确保您的文件未被锁定。

关于java - 无法创建和复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25542291/

相关文章:

android - 模拟惰性构造函数参数

.net - 此异常消息 : Not enough quota is available to process this command 中引用的是什么 'quota'

java - 如何迭代条件生成的列表?

java - 在jsp中显示数组值

开发后Android小部件未显示在列表中

android - 适用于 Unity 的 Facebook SDK 3.1.2 - 在 Android 设备上登录时遇到问题

php - 故意不捕获异常

java - 如何避免异常阴影?

java - 提取特定行java

java - 如何在JList Swing中添加检索到的数据库数据?