继续说sdcard读写文件的情况,这篇博客说的是在sdcard根目录中读写文件,着重的而是多种方式的读写文件。直接给出代码:

FileService.java

package com.llp.classdifine;import java.io.BufferedWriter;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import android.content.Context;import android.os.Environment;public class FileService {private Context context;private String mPath = Environment.getExternalStorageDirectory().getPath()+ "/datas";public FileService(Context context) {this.context = context;}/** * 保存内容到sd卡中(以私有文件形式保存) *  * @param filename *            文件名称 * @param content *            文件内容 * @throws Exception */public void save2sdCard(String fileName, String content) throws Exception {File file = new File(Environment.getExternalStorageDirectory(),fileName);FileOutputStream outputStream = new FileOutputStream(file);outputStream.write(content.getBytes());outputStream.close();}/** * 保存内容(以私有文件形式保存) *  * @param filename *            文件名称 * @param content *            文件内容 * @throws Exception */public void save(String fileName, String content) throws Exception {FileOutputStream outputStream = context.openFileOutput(fileName,Context.MODE_PRIVATE);outputStream.write(content.getBytes());outputStream.close();}/** * 保存内容(以私有文件形式保存+追加) *  * @param filename *            文件名称 * @param content *            文件内容 * @throws Exception */public void saveAppend(String fileName, String content) throws Exception {FileOutputStream outputStream = context.openFileOutput(fileName,Context.MODE_APPEND);outputStream.write(content.getBytes());outputStream.close();}/** * 保存内容(允许其他应用读取) *  * @param filename *            文件名称 * @param content *            文件内容 * @throws Exception */public void saveReadAble(String fileName, String content) throws Exception {FileOutputStream outputStream = context.openFileOutput(fileName,Context.MODE_WORLD_READABLE);outputStream.write(content.getBytes());outputStream.close();}/** * 保存文件到sdcard中某个路径下 * @param fileName * @param content */public void write2Path(String fileName, String content) {File f = new File(mPath + "/" + fileName + ".txt");try {FileOutputStream fileOS = new FileOutputStream(f);try {fileOS.write(content.getBytes());fileOS.close();BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(fileOS));buf.write(content, 0, content.length());buf.flush();buf.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 从sdcard中的某个路径下读取文件 * @param fileName * @return */public String readFromPath(String fileName) {byte[] data = new byte[1024];try {FileInputStream fileIS = new FileInputStream(mPath + "/" + fileName+ ".txt");// BufferedReader buf = new BufferedReader(new// InputStreamReader(in))byte[] buffer = new byte[1024];ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();int len = -1;while ((len = fileIS.read(buffer)) != -1) {// 读取文件到缓冲区byteArrayOutputStream.write(buffer, 0, len);// 写入到内存}data = byteArrayOutputStream.toByteArray();// 将内存中的二进制数据转为数组byteArrayOutputStream.close();fileIS.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return new String(data);}/** * 文件读取 *  * @param fileName * @return * @throws Exception */public String read(String fileName) throws Exception {FileInputStream inputStream = context.openFileInput(fileName);byte[] buffer = new byte[1024];ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();int len = -1;while ((len = inputStream.read(buffer)) != -1) {// 读取文件到缓冲区byteArrayOutputStream.write(buffer, 0, len);// 写入到内存}byte[] data = byteArrayOutputStream.toByteArray();// 将内存中的二进制数据转为数组byteArrayOutputStream.close();inputStream.close();return new String(data);}}

MainActivity.java文件:

package com.example.sdcard;import java.io.File;import com.example.sdcard.FileService;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {/** Called when the activity is first created. */private static final String TAG = "MainActivity";private FileService fileService;String fileName;String content;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);File sd = Environment.getExternalStorageDirectory();String mPath = sd.getPath() + "/datas";File file = new File(mPath);if (!file.exists()) {file.mkdir();}Log.v("test", "file.getPath()" + file.getPath());Button button = (Button) findViewById(R.id.btnsave);Button read = (Button) findViewById(R.id.btnread);fileService = new FileService(this);// File file = this.getFilesDir();// 快速得到文件夹// this.getCacheDir();// 获得缓存文件夹final EditText fileNameText = (EditText) findViewById(R.id.fileName);final EditText contenText = (EditText) findViewById(R.id.content);final EditText readtext = (EditText) findViewById(R.id.contentread);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {fileName = fileNameText.getText().toString();content = contenText.getText().toString();try {// 判断sd卡是否存在手机上并且可以进行读写if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {fileService.write2Path(fileName, content);} else {}} catch (Exception e) {Log.e(TAG, e.toString());}}});read.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {// 判断sd卡是否存在手机上并且可以进行读写if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {fileName = fileNameText.getText().toString();Log.v("test", "fileName" + fileName);readtext.setText(fileService.readFromPath(fileName));} else {}} catch (Exception e) {Log.e(TAG, e.toString());}}});}}


更多相关文章

  1. unity Android(安卓)csv 数据持久化
  2. Android中对标准内核的按键映射以及按键驱动
  3. android 遍历assets下的文件
  4. Android(安卓)数据存储与IO (二)
  5. ubuntu 删除android studio
  6. android获取手机内部存储空间和外部存储空间
  7. Android内存泄露利器(hprof篇)
  8. 将新的驱动源文件添加进android内核进行编译
  9. android 加载布局xml文件时报空指针

随机推荐

  1. Android(安卓)studio 中JNI JAVA和C++互
  2. android的触摸屏事件
  3. Theme.NoTitleBar问题
  4. Android_linux下android platforms下载地
  5. Android(安卓)VNC Server on G1 (PC 远程
  6. 使用SDK Manager更新时出现问题
  7. android拨打电话
  8. Android监听消息通知栏点击事件
  9. Android手机震动抖动效果的实现
  10. checkbox 文字与多选框的对齐问题