2013-02-10 169 views
3

我正在创建一个需要从文件读取数据的应用程序。我最初使用BufferedReaderInputStreamReader从assets文件夹中读取它,但我遇到了内存问题(请参阅Android: File Reading - OutOfMemory Issue)。一个建议是将资产文件夹中的数据复制到内部存储器(而不是SD卡),然后通过RandomAccessFile访问它。于是我抬起头如何从资产内部存储复制文件,我发现2个来源:Android:使用RandomAccessFile从内部存储访问文件

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/RpXiMYV48Ww

http://developergoodies.blogspot.com/2012/11/copy-android-asset-to-internal-storage.html

我决定从第二个使用的代码,并修改它为我的文件。所以它看起来像这样:

public void copyFile() { 
    //Open your file in assets 
    Context context = getApplicationContext(); 
    String destinationFile = context.getFilesDir().getPath() + File.separator + "text.txt"; 

    if (!new File(destinationFile).exists()) { 
     try { 
      copyFromAssetsToStorage(context, "text.txt", destinationFile); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

private void copyStream(InputStream input, OutputStream output) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int length = Input.read(buffer); 
    while (length > 0) { 
     output.write(buffer, 0, length); 
     length = input.read(buffer); 
    } 
} 

private void copyFromAssetsToStorage(Context context, String sourceFile, String destinationFile) throws IOException { 
    InputStream inputStream = context.getAssets().open(sourceFile); 
    OutputStream outputStream = new FileOutputStream(destinationFile); 
    copyStream(inputStream , outputStream); 
    outputStream.flush(); 
    outputStream.close(); 
    inputStream.close(); 
} 

我假设这会将文件复制到应用程序的数据目录中。我无法测试它,因为我希望能够使用RandomAccessFile访问该文件。但是,我从来没有做过这两个任何一个(从资产复制文件和RandomAccessFile),所以我卡住了。这个应用程序的工作已陷入停滞状态,因为这是阻止我完成它的唯一因素。

任何人都可以提供更正,建议和如何使用RandomAccessFile访问数据的正确实现吗? (数据是字符串的列表的长度4-15个字符每行。)

编辑*

private File createCacheFile(Context context, String filename){ 
File cacheFile = new File(context.getCacheDir(), filename); 

    if (cacheFile.exists()) { 
     return cacheFile ; 
    } 

    InputStream inputStream = null; 
    FileOutputStream fileOutputStream = null; 

    try { 

     inputStream = context.getAssets().open(filename); 
     fileOutputStream = new FileOutputStream(cacheFile); 

     int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 
     int length = -1; 
     while ((length = inputStream.read(buffer)) > 0) { 
      fileOutputStream.write(buffer,0,length); 
     } 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    finally { 
     try { 
      fileOutputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      inputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    return cacheFile; 
} 

回答

1

1-从资产文件复制到所述高速缓存目录

此代码只是为了举例说明,你必须做适当的异常处理接近资源

private File createCacheFile(Context context, String filename){ 
    File cacheFile = new File(context.getCacheDir(), filename); 

    if (cacheFile.exists()) { 
     return cacheFile ; 
    } 


    InputStream inputStream = context.getAssets().open(filename); 
    FileOutputStream fileOutputStream = new FileOutputStream(cacheFile); 

    int bufferSize = 1024; 
    byte[] buffer = new byte[bufferSize]; 
    int length = -1; 
    while ((length = inputStream.read(buffer)) > 0) { 
    fileOutputStream.write(buffer,0,length); 
    } 

    fileOutputStream.close(); 
    inputStream.close(); 

    return cacheFile; 
} 

2-打开使用RandomAccessFile

File cacheFile = createCacheFile(context, "text.txt"); 
RandomAccessFile randomAccessFile = new RandomAccessFile(cacheFile, "r"); 

// Process the file 

randomAccessFile.close();  

在一个侧面说明了文件,你应该遵循Java的命名约定,例如你的方法和变量名应以小写字母开始,如copyFromAssetsToStoragedestinationFile

编辑:

你应该做一个独立的try/catch每个close()操作,因此,如果一个失败,其他都还得到执行,并检查他们不是null

finally { 
    try { 
     if(fileOutputStream!=null){ 
      fileOutputStream.close();    
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     if(inputStream!=null){ 
     inputStream.close();  
     }  
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

感谢您的回复。我想确保我得到这个权利。我是否正确实施了try/catch(请参阅编辑)?没有错误,但这并不一定意味着它是正确的。另外,我是否必须执行任何类型的“清理”,例如每次使用后清除缓存? – 2013-02-11 01:42:07

+0

你应该在'try/catch'块之外定义'fileOutputStream'和'inputStream',并在检查它们不为null之后在'final'块中关闭它们(每个'close(你将需要另一个'try/catch' )'操作) – iTech 2013-02-11 01:44:24

+0

哇......呃......好的。我认为这应该都在不同的线程?目前我有一个异步线程设置,所以我只是在async类的doInBackground方法中有第2步代码? – 2013-02-11 01:47:15

相关问题