Android 中未保存文件

发布于 2024-11-25 04:54:05 字数 2802 浏览 1 评论 0原文

我正在尝试创建一个简单的 Android 应用程序,它保存 EditText 表单中的文本并将其存储在内部存储器中。一切似乎都工作正常(出现“已保存”消息),除了当我退出活动并重新启动它时,文件中保存的文本根本不加载。我在这里错过了什么吗?

public class ModifyInfo extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);

    Bundle extras = getIntent().getExtras(); 
    if(extras != null){
        int dayNum = extras.getInt("day");
        String dayName = "";

        switch(dayNum){
        case 1:
            dayName = this.getString(R.string.dayMon);
            break;
        case 2:
            dayName = this.getString(R.string.dayTue);
            break;
        case 3:
            dayName = this.getString(R.string.dayWed);
            break;
        case 4:
            dayName = this.getString(R.string.dayThu);
            break;
        case 5:
            dayName = this.getString(R.string.dayFri);
            break;
        case 6:
            dayName = this.getString(R.string.daySat);
            break;
        default:
            dayName = this.getString(R.string.daySun);
            break;
        }

        final TextView dayText = (TextView) findViewById(R.id.dayName);
        final TextView editData = (TextView) findViewById(R.id.editData);
        final Button save = (Button) findViewById(R.id.buttonSave);
        final Button clear = (Button) findViewById(R.id.buttonClear);
        final String Day = dayName;

        dayText.setText(dayName);

        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editData.setText("");
            }
        });

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

                try {
                    String string = editData.getText().toString();

                    FileOutputStream fos = openFileOutput(FILEOUTPUT, Context.MODE_PRIVATE);
                    fos.write(string.getBytes());
                    fos.close();

                    Toast.makeText(ModifyInfo.this, "Saved", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(ModifyInfo.this, "Save error", Toast.LENGTH_SHORT).show();   
                }
            }
        });

        File FILEINPUT = new File(Day + ".txt");

        try {
            BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
            String line;

            while ((line = bfr.readLine()) != null)
            {
                editData.setText(line);
            }

            bfr.close();

        } catch (Exception e) {
            editData.setText("");
        }   
    }   
}

}

I'm trying to create a simple Android app which saves a text from an EditText form and store it in internal memory. Everything seems to work fine (the "Saved" message appears) except when I quit the activity and restart it, the saved text from file does not load at all. Am I missing something here?

public class ModifyInfo extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);

    Bundle extras = getIntent().getExtras(); 
    if(extras != null){
        int dayNum = extras.getInt("day");
        String dayName = "";

        switch(dayNum){
        case 1:
            dayName = this.getString(R.string.dayMon);
            break;
        case 2:
            dayName = this.getString(R.string.dayTue);
            break;
        case 3:
            dayName = this.getString(R.string.dayWed);
            break;
        case 4:
            dayName = this.getString(R.string.dayThu);
            break;
        case 5:
            dayName = this.getString(R.string.dayFri);
            break;
        case 6:
            dayName = this.getString(R.string.daySat);
            break;
        default:
            dayName = this.getString(R.string.daySun);
            break;
        }

        final TextView dayText = (TextView) findViewById(R.id.dayName);
        final TextView editData = (TextView) findViewById(R.id.editData);
        final Button save = (Button) findViewById(R.id.buttonSave);
        final Button clear = (Button) findViewById(R.id.buttonClear);
        final String Day = dayName;

        dayText.setText(dayName);

        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editData.setText("");
            }
        });

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

                try {
                    String string = editData.getText().toString();

                    FileOutputStream fos = openFileOutput(FILEOUTPUT, Context.MODE_PRIVATE);
                    fos.write(string.getBytes());
                    fos.close();

                    Toast.makeText(ModifyInfo.this, "Saved", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(ModifyInfo.this, "Save error", Toast.LENGTH_SHORT).show();   
                }
            }
        });

        File FILEINPUT = new File(Day + ".txt");

        try {
            BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
            String line;

            while ((line = bfr.readLine()) != null)
            {
                editData.setText(line);
            }

            bfr.close();

        } catch (Exception e) {
            editData.setText("");
        }   
    }   
}

}

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

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

发布评论

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

评论(2

美煞众生 2024-12-02 04:54:05

您可能会从文件的最后一行读取 \n 或此类空格,因为您正在调用 editData.setText(line); ,这会将文本重置为当前行。
或者您也可能遇到异常,在这种情况下,您可以通过清除 editText 来处理该异常。

它应该是这样的:

try {
        BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
        String line;
        String fullText = "";

        while ((line = bfr.readLine()) != null)
        {
            fullText += line;
        }

        bfr.close();

        editData.setText(fullText);

    } catch (Exception e) {
        editData.setText("");
    }   

根据您的澄清,您应该检查是否正确打开文件,如下所示: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

要从内部存储读取文件:

1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
2. Read bytes from the file with read().
3. Then close the stream with close().

您正在尝试打开使用改为 BufferedReader。

You may be reading \n or such whitespace from the last line of the file, since you are calling editData.setText(line); , which resets the text to current line .
Or you may also be experiencing an Exception, in that case you are handling that Exception by clearing editText.

It should be like this instead:

try {
        BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
        String line;
        String fullText = "";

        while ((line = bfr.readLine()) != null)
        {
            fullText += line;
        }

        bfr.close();

        editData.setText(fullText);

    } catch (Exception e) {
        editData.setText("");
    }   

According to your clarification, you should check that you open your file correctly as stated in: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

To read a file from internal storage:

1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
2. Read bytes from the file with read().
3. Then close the stream with close().

You are trying to open using BufferedReader instead.

壹場煙雨 2024-12-02 04:54:05

Android 可能会锁定某些文件夹中的文件,并且您无法修改它们。您可以使用共享首选项或静态对象。

It is possible that android locks files from some folders and you can not modify them. You can use sharedpreferences or static objects.

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