在Android中获取文件目录内文件的路径

10

我有一个名为abikor.txt的文件,它位于私有应用程序文件夹中的files目录内:

/data/data/myapp/files/abikor.txt

如何获取此文件的路径?

我看到了以下问题,但它们并没有解决我的问题。

(通常情况下,如何获取存储在私有应用程序目录下的/files目录中的文件路径?)

顺祝商祺

1个回答

16

对于内部存储路径,您需要指定类似于此的内容

String path = context.getFilesDir().getAbsolutePath();

然后从路径创建一个文件对象

File file = new File(path + "/abikor.txt");

如果您想从中读取文件,则

Read the file from it

int length = (int) file.length();

byte[] bytes = new byte[length];

FileInputStream in = new FileInputStream(file);
try {
    in.read(bytes);
} finally {
    in.close();
}

String contents = new String(bytes);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接