从android手机内存中读取文本文件

时间:2011-01-11 06:26:34

标签: android

我只是想在手机内存中创建一个文本文件,并且必须阅读其内容才能显示。现在我创建了一个文本文件。但它不存在于路径数据/ data / package-name / file name.txt&它没有在模拟器上显示内容。

我的代码是..

public class PhonememAct extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=(TextView)findViewById(R.id.tv);

        FileOutputStream fos = null;
        try {
            fos = openFileOutput("Test.txt", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        try {
            fos.write("Hai..".getBytes());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        FileInputStream fis = null;
        try {
            fis = openFileInput("Test.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int c;

        try {
            while((c=fis.read())!=-1)
                    {
                        tv.setText(c);
                        setContentView(tv);

                        //k += (char)c;
                    }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }
}

先谢谢。

5 个答案:

答案 0 :(得分:7)

如果您只是尝试写/读文本,则无需使用输入/输出流。

使用FileWriter将文本写入文件,使用BufferedReader从文件中读取文本 - 这样更简单。这很有效......

try {
    File myDir = new File(getFilesDir().getAbsolutePath());
    String s = "";

    FileWriter fw = new FileWriter(myDir + "/Test.txt");
    fw.write("Hello World");
    fw.close();

    BufferedReader br = new BufferedReader(new FileReader(myDir + "/Test.txt"));
    s = br.readLine();

    // Set TextView text here using tv.setText(s);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();

}

答案 1 :(得分:1)

    //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"file.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
    text.append(line);
    text.append('\n');
    }
    }
    catch (IOException e) {
    //You'll need to add proper error handling here
    }

    //Find the view by its id
    TextView tv = (TextView)findViewById(R.id.text_view);

    //Set the text
    tv.setText(text);

答案 2 :(得分:1)

//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext(); 

filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath, fileName);
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));   
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {

}
return text.toString(); //the output text from file.

答案 3 :(得分:0)

这可能不是您问题的答案 我想,你需要正确使用try-catch。 想象一下openFileInput()调用失败,接下来你在空对象上调用fos.write()fos.close()

稍后会在fis.read()fis.close()中看到同样的事情。

您需要在一个try-catch块中包含openFileInput()fos.write()fos.close()。 “fis”也需要进行类似的更改。

先试试这个!

答案 4 :(得分:0)

您可以使用进行尝试。

    public static void persistAll(Context ctx, List<myObject> myObjects) {

    // save data to file
    FileOutputStream out = null;

    try {
           out = ctx.openFileOutput("file.obj",

           Context.MODE_PRIVATE);
           ObjectOutputStream objOut = new ObjectOutputStream(out);
           objOut.writeObject(myObjects);
           } catch (Exception e) {
        e.printStackTrace();
           } finally {
           try {
               out.close();
           } catch (IOException e) {
               e.printStackTrace();
           } 
       }
   }

这样对我来说很好。保存为文本应该没有那么不同,但我没有Java IDE在这里测试工作。 希望这有帮助!