android - 在Android的内部存储特定文件夹中写入文件

标签 android

我想在内部存储中的特定文件夹中写入文件,在创建 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/

相关文章:

android - 软键盘不隐藏在 fragment 中。如何隐藏键盘?

android - 仅使用 GPS 的地理围栏在 Android 中不起作用

android - 将文件私下保存到内部存储

Android SQLite 数据库插入/选择不工作

android - Firebase 归因跟踪

Android:从以前的位置恢复应用程序

android - Robolectric 与 test.R.java

带有文本的Android自定义后退按钮

android - 运行监听 android 日历事件的服务

java - 在 2 个 ListView 上方添加标题/TextView