Android存储(二):openFileInput和openFileOutput私有目录下文件
16lz
2021-01-26
openFileInput和openFileOutput用于处理应用程序私有目录下的指定私有文件的读取或写入数据
在Android中,可以通过Context.openFileInput和Context.openFileOutput来分别获取FileInputStream和FileOutputStream。
openFileInput(String fileName); 打开应用程序私有目录下的指定私有文件以读入数据,返回一个FileInputStream对象。
openFileOutput(String name,int mode);打开应用程序私有目录下的指定私有文件以写入数据,返回一个FileOutputStream对象,如果文件不存在就创建这个文件。
第二参数mode用于指定操作模式,有四种模式:
-
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。
-
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件里追加内容,否则就创建新文件。
-
Context.MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
-
Context.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput(“itcast.txt”, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
除此之外,Context还提供了如下方法来访问应用程序的数据文件夹:
- File Context.getFilesDir(),该方法返回/data/data/youPackageName/files的File对象,即应用程序的绝对路径
- getDir(String name,int mode): 在应用程序的数据文件夹下获取或创建name对应的子目录
- String【】 Context.fileList(),返回files下所有的文件名,返回的是String[]对象。
- Context.deleteFile(String),删除files下指定名称的文件。
5.Context.getCacheDir(),该方法返回/data/data/youPackageName/cache的File对象。
实例1:openFileInput和openFileOutput读写文件
//写入文件 public void fileWrite(String filestr){ boolean flag=false; try { FileOutputStream fos = openFileOutput("a.txt",MODE_PRIVATE); fos.write(filestr.getBytes()); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //读取文件内容 public String fileRead(){ String content = null; try { FileInputStream fis = openFileInput("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len=0; while((len=fis.read(buffer))!=-1){ baos.write(buffer,0,len); } content = baos.toString(); fis.close(); baos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return content; }
实例二:文件存储方式实现登录保存用户帐号和密码的操作
import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity{ private Button btn1 ; private EditText ed_name ; private EditText ed_pass ; //定义一个文件输入流对象 FileOutputStream out = null ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed_name =(EditText)findViewById(R.id.ed_name); ed_pass = (EditText)findViewById(R.id.ed_pass); readData(); btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String name = ed_name.getText().toString() ; String pass = ed_pass.getText().toString() ; try { out = MainActivity.this.openFileOutput("data", MODE_PRIVATE); out.write((name+"##"+pass).getBytes()) ; } catch (Exception e) { e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) {e.printStackTrace();} } } }); } public void readData(){ FileInputStream input =null; BufferedReader read = null; try { input = this.openFileInput("data"); if(input!=null){ //获取一个字符流对象 read = new BufferedReader(new InputStreamReader(input)); String[] str = read.readLine().split("##"); ed_name.setText(str[0]); ed_pass.setText(str[1]); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { input.close() ; read.close(); } catch (IOException e) {e.printStackTrace();} } } }
更多相关文章
- Ubuntu13.04环境下载、编译Android源代码
- receiver定制自动启动一个程序
- node.js+android(使用HttpURLConnection和HttpClient)实现文件上
- [cocos2dx] cocosdx编译工程那些事
- Android(安卓)SDK API 13升级指南
- Android视频播放框架一、Vitamio
- android aidl 传递parcelable
- JNI综合实验一:LED点亮+IO电平读取
- View编程(5): 自定义View_01_ApiDemo源码研究