Android:DataInputStream & DataOutputstream 没有看到我的目录

发布于 2025-01-08 05:10:35 字数 1518 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-01-15 05:10:35

您的实施是正确的。但请注意,您必须验证您的手机或模拟器上是否有可用的 SD 卡。

为了方便起见,您可以尝试是否

Environment.getExternalStorageDirectory().getAbsoluteFile()

返回现有文件。

如果文件存在,请检查权限。如果您想使用外部存储,则需要在 AndroidManifest.xml 中获得以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

希望它有所帮助。

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

Environment.getExternalStorageDirectory().getAbsoluteFile()

returns existing file.

If the file exists, check the permission. Following permission is needed in AndroidManifest.xml, if you want to use external storage:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文