Android 中存储数据的问题

发布于 2024-11-25 04:32:58 字数 2842 浏览 2 评论 0原文

我有一个按钮,它将以上面的 EditText 表单写入的数据存储到内存中,但由于某种原因,代码不起作用。它甚至不显示 toast 消息。

final Button save = (Button) findViewById(R.id.buttonSave);
final String Day = dayName;

save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String FILEOUTPUT = Day;
                BufferedWriter bfw;

                try {
                    bfw = new BufferedWriter (new FileWriter(FILEOUTPUT));
                    Scanner scan = new Scanner(new File(editData.getText().toString()));

                    bfw.write(scan.nextLine());
                    bfw.close();
                    Toast.makeText(ModifyInfo.this, "Saved", Toast.LENGTH_SHORT);
                } catch (Exception e) {
                    Toast.makeText(ModifyInfo.this, "Save error", Toast.LENGTH_SHORT);  
                }
            }
        });

07-19 20:00:59.677: 错误/文件保存(341):保存失败

07-19 20:00:59.677: 错误/文件保存 (341): java.io.FileNotFoundException:/Monday(只读文件系统)

07-19 20:00:59.677: 错误/文件保存 (341):位于 org.apache.harmony.luni.platform.OSFileSystem.open(本机方法)

07-19 20:00:59.677: 错误/文件保存 (341):位于 dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)

07-19 20:00:59.677: 错误/文件保存 (341):位于 java.io.FileOutputStream.(FileOutputStream.java:94)

07-19 20:00:59.677: 错误/文件保存 (341):位于 java.io.FileOutputStream.(FileOutputStream.java:66)

07-19 20:00:59.677: 错误/文件保存 (341):位于 java.io.FileWriter.(FileWriter.java:80)

07-19 20:00:59.677: 错误/文件保存 (341):位于 com.geft.timetable.ModifyInfo$2.onClick(ModifyInfo.java:77)

07-19 20:00:59.677: 错误/文件保存 (341):位于 android.view.View.performClick(View.java:2485)

07-19 20:00:59.677: 错误/文件保存 (341):位于 android.view.View$PerformClick.run(View.java:9080)

07-19 20:00:59.677: 错误/文件保存 (341):位于 android.os.Handler.handleCallback(Handler.java:587)

07-19 20:00:59.677: 错误/文件保存 (341):位于 android.os.Handler.dispatchMessage(Handler.java:92)

07-19 20:00:59.677: 错误/文件保存 (341):位于 android.os.Looper.loop(Looper.java:123)

07-19 20:00:59.677: 错误/文件保存 (341):位于 android.app.ActivityThread.main(ActivityThread.java:3683)

07-19 20:00:59.677: 错误/文件保存 (341):位于 java.lang.reflect.Method.invokeNative(原生方法)

07-19 20:00:59.677: 错误/文件保存 (341):位于 java.lang.reflect.Method.invoke(Method.java:507)

07-19 20:00:59.677: 错误/文件保存 (341):位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

07-19 20:00:59.677: 错误/文件保存 (341):位于 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

07-19 20:00:59.677: 错误/文件保存 (341):位于 dalvik.system.NativeStart.main(本机方法)

I have a button which stores data written in the EditText form above it to internal memory, but for some reason the code doesn't work. It doesn't even display the toast message.

final Button save = (Button) findViewById(R.id.buttonSave);
final String Day = dayName;

save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String FILEOUTPUT = Day;
                BufferedWriter bfw;

                try {
                    bfw = new BufferedWriter (new FileWriter(FILEOUTPUT));
                    Scanner scan = new Scanner(new File(editData.getText().toString()));

                    bfw.write(scan.nextLine());
                    bfw.close();
                    Toast.makeText(ModifyInfo.this, "Saved", Toast.LENGTH_SHORT);
                } catch (Exception e) {
                    Toast.makeText(ModifyInfo.this, "Save error", Toast.LENGTH_SHORT);  
                }
            }
        });

07-19 20:00:59.677: ERROR/File Save(341): Fail to save

07-19 20:00:59.677: ERROR/File Save(341):
java.io.FileNotFoundException: /Monday (Read-only file system)

07-19 20:00:59.677: ERROR/File Save(341): at
org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)

07-19 20:00:59.677: ERROR/File Save(341): at
dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)

07-19 20:00:59.677: ERROR/File Save(341): at
java.io.FileOutputStream.(FileOutputStream.java:94)

07-19 20:00:59.677: ERROR/File Save(341): at
java.io.FileOutputStream.(FileOutputStream.java:66)

07-19 20:00:59.677: ERROR/File Save(341): at
java.io.FileWriter.(FileWriter.java:80)

07-19 20:00:59.677: ERROR/File Save(341): at
com.geft.timetable.ModifyInfo$2.onClick(ModifyInfo.java:77)

07-19 20:00:59.677: ERROR/File Save(341): at
android.view.View.performClick(View.java:2485)

07-19 20:00:59.677: ERROR/File Save(341): at
android.view.View$PerformClick.run(View.java:9080)

07-19 20:00:59.677: ERROR/File Save(341): at
android.os.Handler.handleCallback(Handler.java:587)

07-19 20:00:59.677: ERROR/File Save(341): at
android.os.Handler.dispatchMessage(Handler.java:92)

07-19 20:00:59.677: ERROR/File Save(341): at
android.os.Looper.loop(Looper.java:123)

07-19 20:00:59.677: ERROR/File Save(341): at
android.app.ActivityThread.main(ActivityThread.java:3683)

07-19 20:00:59.677: ERROR/File Save(341): at
java.lang.reflect.Method.invokeNative(Native Method)

07-19 20:00:59.677: ERROR/File Save(341): at
java.lang.reflect.Method.invoke(Method.java:507)

07-19 20:00:59.677: ERROR/File Save(341): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

07-19 20:00:59.677: ERROR/File Save(341): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

07-19 20:00:59.677: ERROR/File Save(341): at
dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

万劫不复 2024-12-02 04:32:58

您对这一行所做的操作可能是错误的 editData.getText().toString()。如果您的 editData 是 EditText 那么您的程序将需要其中的文件名。我认为您没有提供文件name 在这里。您应该在此处输入应保存在文件中的字符串。

what you doing with this line is wrong probably editData.getText().toString().If your editData is an EditText then your program will need a file name from it.I think you are not providing file name here.You are supposed to enter the string here that should be saved in the file.

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