我想在内部存储中的特定文件夹中写入文件,在创建 fileoutputStream 时出现错误。 下面是我的代码:
String filePath = ctx.getFilesDir().getPath().toString() +"/"+folderName;
Log.d("filePath",filePath);
file= new File(filePath, filename);
final FileOutputStream fileOutput = new FileOutputStream(file);
最佳答案
你可以走这条路。
File directory = new File("path_to_directory");
try {
if(!file.exists()) {
directory.createNewFile();
}
File dataFile = new File(directory, "Your File Name");
FileOutputStream stream = new FileOutputStream(dataFile, true); // true if append is required.
stream.write();
stream.flush()
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (null != stream) {
stream.close();
}
这里path_to_directory = Environment.getExternalStorageDirectory() + File.seperator + "FolderName";
或者
path_to_directory = ctx.getFilesDirectory() + File.seperator + "FolderName";
顺便说一句,在获得 root 权限之前,您无法在 Android 的内部存储中创建文件夹。它肯定会给你 IOException,因为没有 root,android 内部 FS 是只读的。所以请注意这一点。
谢谢, 快乐编码:-)
关于android - 在Android的内部存储特定文件夹中写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538689/