gpt4 book ai didi

Android:使用 RandomAccessFile 从内部存储访问文件

转载 作者:太空狗 更新时间:2023-10-29 14:23:14 24 4
gpt4 key购买 nike

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

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 访问该文件。但是,我从来没有做过这两项中的任何一项(从 Assets 和 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-将文件从assets复制到缓存目录

这段代码只是为了说明,你必须做适当的异常处理关闭资源

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

编辑:

您应该为每个 close() 操作创建一个单独的 try/catch,这样如果一个失败,另一个仍然会被执行并检查它们不是

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

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

关于Android:使用 RandomAccessFile 从内部存储访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14801876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com