File

Android中文件存储操作:
  • Activity的openFileOutput()方法可以用于把数据输出到文件中
  • 创建的文件保存在/data/data/<package name>/files目录
  • 实现过程与在java 中保存数据到文件一样

  File file = this.getFilesDir();
  //返回当前默认的数据存储目录
  File file = this.getFilesDir();
  //返回当前默认的缓存文件的存储目录
  File file = this.getDir("test", MODE_PRIVATE);
  //返回名为test的目录,不存在则新建
  File file = this.getExternalCacheDir();
  //可以得到外部的存储位置 该位置的数据跟内置使用一样

FileInputStream和FileOutputStream
example:*------------------------------------------------------------------------------------
 et = (EditText) findViewById(R.id.editText1);
  bt= (Button) findViewById(R.id.button1);
  tv = (TextView) findViewById(R.id.textView1);
  bt.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    writeFile(et.getText().toString());
    tv.setText(readFile());
   }
  });
 }
 public void writeFile(String content)
 {
  FileOutputStream fos = null;
  try {
   fos = openFileOutput("a.txt", MODE_PRIVATE);
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  try {
   fos.write(content.getBytes());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   fos.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public String readFile()
 {
  String text = null;
  FileInputStream fis = null;
  try {
   fis = openFileInput("a.txt");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  ByteArrayOutputStream baos= new ByteArrayOutputStream();
  byte []buffer = new byte[1024];
  int len =0;
  try {
   while((len=fis.read(buffer))!=-1)
   {
    baos.write(buffer, 0, len);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  text = baos.toString();
  try {
   fis.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   baos.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return text;
 } et = (EditText) findViewById(R.id.editText1);
  bt= (Button) findViewById(R.id.button1);
  tv = (TextView) findViewById(R.id.textView1);
  bt.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    writeFile(et.getText().toString());
    tv.setText(readFile());
   }
  });
 }
 public void writeFile(String content)
 {
  FileOutputStream fos = null;
  try {
   fos = openFileOutput("a.txt", MODE_PRIVATE);
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  try {
   fos.write(content.getBytes());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   fos.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public String readFile()
 {
  String text = null;
  FileInputStream fis = null;
  try {
   fis = openFileInput("a.txt");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  ByteArrayOutputStream baos= new ByteArrayOutputStream();
  byte []buffer = new byte[1024];
  int len =0;
  try {
   while((len=fis.read(buffer))!=-1)
   {
    baos.write(buffer, 0, len);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  text = baos.toString();
  try {
   fis.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   baos.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return text;
}

你可能感兴趣的:(android)