Android:DataInputStream & DataOutputstream 没有看到我的目录
我正在尝试将 int 值保存在外部存储上的文本文件中。当我尝试使用 saveAudio() 函数时,出现 FileNotFoundException。我做错了什么?我正在 Android 模拟器中运行该程序。
File externalStorageDir = new File (Environment.getExternalStorageDirectory().getAbsoluteFile(),"audiosettings.txt");
/**Stores the game volume level*/
int gameVolume_level;
/**Stores the music volume level*/
int musicVolume_level;
/**Stores the GUI volume level*/
int guiVolume_level;
private void loadAudio() {
try {
DataInputStream dis = new DataInputStream(new FileInputStream(externalStorageDir));
gameVolume_level = dis.readInt();
dis.readChar();
musicVolume_level = dis.readInt();
dis.readChar();
guiVolume_level = dis.readInt();
dis.readChar();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveAudio() {
DataOutputStream dos;
try {
dos = new DataOutputStream(new FileOutputStream(externalStorageDir));
dos.writeInt(gameVolume_level);
dos.writeChar('\t');
dos.writeInt(musicVolume_level);
dos.writeChar('\t');
dos.writeInt(guiVolume_level);
dos.writeChar('\n');
dos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm trying to save int values inside a text file on external storage. When I tried to use the saveAudio() function, I get a FileNotFoundException. What am I doing wrong? I'm running the program in an android emulator.
File externalStorageDir = new File (Environment.getExternalStorageDirectory().getAbsoluteFile(),"audiosettings.txt");
/**Stores the game volume level*/
int gameVolume_level;
/**Stores the music volume level*/
int musicVolume_level;
/**Stores the GUI volume level*/
int guiVolume_level;
private void loadAudio() {
try {
DataInputStream dis = new DataInputStream(new FileInputStream(externalStorageDir));
gameVolume_level = dis.readInt();
dis.readChar();
musicVolume_level = dis.readInt();
dis.readChar();
guiVolume_level = dis.readInt();
dis.readChar();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveAudio() {
DataOutputStream dos;
try {
dos = new DataOutputStream(new FileOutputStream(externalStorageDir));
dos.writeInt(gameVolume_level);
dos.writeChar('\t');
dos.writeInt(musicVolume_level);
dos.writeChar('\t');
dos.writeInt(guiVolume_level);
dos.writeChar('\n');
dos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的实施是正确的。但请注意,您必须验证您的手机或模拟器上是否有可用的 SD 卡。
为了方便起见,您可以尝试是否
返回现有文件。
如果文件存在,请检查权限。如果您想使用外部存储,则需要在 AndroidManifest.xml 中获得以下权限:
希望它有所帮助。
Your implementation is right. But notice that you have to verify that a SD card is available on your phone or emulator.
To make it easier, you can try whether
returns existing file.
If the file exists, check the permission. Following permission is needed in AndroidManifest.xml, if you want to use external storage:
Hope it helps.