码迷,mamicode.com
首页 > 其他好文 > 详细

File

时间:2016-05-07 08:05:48      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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

File

标签:

原文地址:http://blog.csdn.net/qq_29627885/article/details/51335544

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!