从手机内存中的Andr​​oid阅读的文本文件文本文件、内存、手机、Andr

由网友(煮酒论余生)分享简介:我只是想创建一个文本文件到手机内存并读取其内容display.Now我创建了一个文本file.But它不是$​​ P $ psent路径数据/数据​​/软件包名/文件名。 TXT和放大器;它并没有显示在模拟器的内容。I just wanna create a text file into phone memory a...

我只是想创建一个文本文件到手机内存并读取其内容display.Now我创建了一个文本file.But它不是$​​ P $ psent路径数据/数据​​/软件包名/文件名。 TXT和放大器;它并没有显示在模拟器的内容。

I just wanna create a text file into phone memory and have to read its content to display.Now i created a text file.But its not present in the path data/data/package-name/file name.txt & it didn't display the content on emulator.

我的code是..

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();
            }


    }
}

由于在副词。

Thanks in adv.

推荐答案

您不需要使用的输入/输出流,如果你只是想读/写的文字。

You don't need to use input/output streams if you are simply trying to write/read text.

使用的FileWriter写文本文件和BufferedReader从文件中读取文本 - 这是非常简单的。这工作完全...

Use FileWriter to write text to a file and BufferedReader to read text from a file - it's much simpler. This works perfectly...

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();

}

阅读全文

相关推荐

最新文章