【问题标题】:Can`t create and copy the file无法创建和复制文件
【发布时间】:2014-10-21 22:21:32
【问题描述】:

我的程序应该如下工作:

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

但由于某种原因,我得到了一个例外。为什么?

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

【问题讨论】:

  • 你是否获得了写文件的权限?
  • 是的,当然,我添加了所有权限
  • 显示你的 manifest.xml 文件
  • 文档说 createNewFile() 只有在无法创建文件时才会抛出异常。也许你应该检查它是否已经存在?
  • 或者如果它是只读的?

标签: java android exception android-asynctask


【解决方案1】:

在创建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 以确保您的文件未被锁定。

【讨论】:

  • 我还是一样的例外
  • 那么您的代码是否输入了 tempFile.creaNewFile() 行,这意味着该文件不存在?你能调试你的代码并告诉我异常是在哪里抛出的吗?
  • 调用 createNewFile() 时发生异常。通过 Total Comander,我看到该文件不存在于程序中
猜你喜欢
  • 1970-01-01
  • 2023-02-06
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多