openFileOutput将文件保存在。data/data/<package name>/files目录下。
在使用openFileInput方法获取InputStream对象来读取文件中的数据时。只需要指定文件名即可。InputStream is=openFileInput("file.txt");
下面是使用openFileOutput和outFileInput方法获取OutputStream及InputStream对象来读取文件的完整代码。
、、
写入内容
OutputStream os=openFileOutput("file.txt",Activity.MODE_PRIVATE);
String str1="书名:Android开发完全讲义";
os.write(str1.getBytes("utf-8"));//写入字符串
//void write( int b ); //往流中写一个字节b
//void write( byte b[ ] ); //往流中写一个字节数组b
//void write( byte b[ ], int off, int len ); //把字节数组b中从下标off开始,长度为len的字节写入流中
//void write( byte b[ ] ); //往流中写一个字节数组b
//void write( byte b[ ], int off, int len ); //把字节数组b中从下标off开始,长度为len的字节写入流中
os.close;//关闭
//读取文件的内容
InputStream is =openFileInput("file.txt");//打开文件夹
byte[] buffer=new byte[100];
int byteCount=is.read(buffer);
String str2=new String(buffer,0,byteCount,"utf-8");
TextView textView=(TextView)findViewById(R.id.textview);
textView.setText(str2);
is.close();