保存到文件Android

时间:2014-04-06 19:26:18

标签: android file file-io

我无法调试我的一些代码。它是一个简单的笔记存储应用程序,它尝试将字符串存储在后台文件中。但是当我在我的平板电脑上运行应用程序时,我得到了确认代码运行的吐司,但是我无法在平板电脑内部存储上找到该文件以证明它已被存储?

我的代码:

public void onClick(View v) {
            //Gets the note detail from the fields.
            noteContent = content.getText().toString();
            noteTitle = title.getText().toString();
            Date date = new Date();

            if (noteContent.length() == 0 || noteTitle.length() == 0) {
                Toast.makeText(getApplicationContext(), "Please fill in both note title and content before saving",
                                Toast.LENGTH_LONG).show();
            } else {

            //Adds the notes together as an object of note.
            note createdNote = new note(noteContent, noteTitle);
            //Adds the note to the object array.
            noteArray.add(createdNote);
            //Testing
            note test = noteArray.get(0);
            String testTitle = test.getTitle();
            String testContent = test.getContent();
            System.out.println(testTitle);
            System.out.println(testContent);
            //Concatenating string.
            String noteString = testTitle + "-" + testContent;


            try {
                FileOutputStream FO = openFileOutput(fileName, MODE_WORLD_READABLE);
                OutputStreamWriter osw = new OutputStreamWriter(FO);
                try {
                    System.out.println(noteString);
                    osw.write(noteString);
                    osw.flush();
                    osw.close();
                    Toast.makeText(getBaseContext(), "NOTE_SAVED", Toast.LENGTH_LONG).show();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

我还包括读/写权限:s

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

2 个答案:

答案 0 :(得分:0)

使用DDMS的文件资源管理器并浏览到/ data / data / YOUR_PACKAGE / files /&lt; - 您可以找到您的文件。 有关更多选项,请参阅此处Android: can not write to a txt file

答案 1 :(得分:0)

因为您正在写入内部存储器,所以您不需要这些权限。对于这个问题,你为什么要打开这样的文件:FileOutputStream FO = openFileOutput(fileName, MODE_WORLD_READABLE);