我正在尝试将一个文件写入我的手机(以及将来用户的手机)的内部设备存储中。我在看2016年的视频教程(https://www.youtube.com/watch?v=EhBBWVydcH8),其中展示了他如何非常简单地将输出写入文件。如果你想看他的代码,跳到8:23。
无论如何,我基本上尝试了他的代码,然后由于它不起作用,我想我会搜索周围。
显然,要创建一个文件,我需要以下几行代码:
String filename = "textfile.txt";
File file = new File(filename);
file.mkdirs();
file.createNewFile();在第二行file.createNewFile()上,我得到了下面的错误:
java.io.IOException: Read-only file system
at java.io.UnixFileSystem.createFileExclusivel
at java.io.UnixFileSystem.createFileExclusivel
at java.io.File.createNewFile(File.java:948)
etc......然后,如果我仅仅使用本教程中的代码行来运行我的代码,我会得到一个空指针。
代码:
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(IDNum.getBytes());
fos.close();
System.out.println("Wrote STuff Outputtt?");
} catch (Exception e) {
e.printStackTrace();
}错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:199)
at com.lecconnect.lockoutdemo.FileManager.AddUser(FileManager.java:37)第37行是try/catch中的第一行。
如果您需要任何其他信息来帮助我,请让我知道。非常感谢您的回复。
https://stackoverflow.com/questions/44339963
复制相似问题