`
1028826685
  • 浏览: 922804 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类

android 操作文件

阅读更多
package com.liyong;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileService {

private Context context;

public FileService(Context context) {
  super();
  this.context = context;
}
/**
  * 写入文件到SD卡
  * @throws IOException
  */
public void saveToSD(String fileNameStr, String fileContentStr) throws IOException{
 
  //备注:Java File类能够创建文件或者文件夹,但是不能两个一起创建
  File file = new File("/mnt/sdcard/myfiles");
//  if(!file.exists())
//  {
//   file.mkdirs();
//  }
//   File sd=Environment.getExternalStorageDirectory();
//       String path=sd.getPath()+"/myfiles";
//       File file=new File(path);
//       if(file.exists()){
//           file.delete();
//       }
      if(!file.exists()){
       file.mkdir();
      }
      File file1 = new File(file, fileNameStr);


  FileOutputStream fos = new FileOutputStream(file1);
  fos.write(fileContentStr.getBytes());
  fos.close();
}

/**
  * 保存文件到手机
  * @param fileNameStr 文件名
  * @param fileContentStr 文件内容
  * @throws IOException
  */
public void save(String fileNameStr, String fileContentStr) throws IOException {
  //私有操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,另外采用私有操作模式创建的文件,写入文件中的内容会覆盖原文件的内容
  FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_PRIVATE);
  fos.write(fileContentStr.getBytes());
  fos.close();
}

public void saveAppend(String fileNameStr, String fileContentStr) throws IOException {
  //追加操作模式:不覆盖源文件,但是同样其它应用无法访问该文件
  FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_APPEND);
  fos.write(fileContentStr.getBytes());
  fos.close();
}

public void saveReadable(String fileNameStr, String fileContentStr) throws IOException {
  //读取操作模式:可以被其它应用读取,但不能写入
  FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_WORLD_READABLE);
  fos.write(fileContentStr.getBytes());
  fos.close();
}

public void saveWriteable(String fileNameStr, String fileContentStr) throws IOException {
  //写入操作模式:可以被其它应用写入,但不能读取
  FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_WORLD_WRITEABLE);
  fos.write(fileContentStr.getBytes());
  fos.close();
}

public void saveReadWriteable(String fileNameStr, String fileContentStr) throws IOException {
  //读写操作模式:可以被其它应用读写
  FileOutputStream fos = context.openFileOutput(fileNameStr,
    context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE);
  fos.write(fileContentStr.getBytes());
  fos.close();
}


/**
  * 读取文件内容
  * @param fileNamestr 文件名
  * @return
  * @throws IOException
  */
public String read(String fileNamestr) throws IOException
{
  FileInputStream fis = context.openFileInput(fileNamestr);
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while((len = fis.read(buffer)) != -1){
   bos.write(buffer,0,len);
  }
  byte[] data = bos.toByteArray();
 
 
  return new String(data);
}

}


分享到:
评论

相关推荐

    Android 文件读写操作

    Android - 文件读写操作 方法总结

    Android文件操作实例

    android 文件 操作 例子 源码 android 文件 操作 例子 源码 android 文件 操作 例子 源码 android 文件 操作 例子 源码 android 文件 操作 例子 源码

    Android文件操作工具类

    文件操作工具类,包含生成保存,复制,删除,读取,获取文件名,获取文件列表等等,只有你想不到,没有你找不到的Android端工具类,复制到项目中可直接使用

    Android的文件操作

    android实际开发中遇到的关于文件操作方面的总结,以word文档形式展现出来。个人认为比较全面~

    Android 读写文件实例

    Android 读写文件实例,包括 SD 卡读写和 内部存储读写。 文件目录获取等。

    Unity中Android的文件操作

    原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。

    Android文件管理器源码

    Android文件管理器(增加了文件夹复制移动,下载资源暂停删除等,以及复制过程中的可视化进程)是一个基于Android开发的应用,包含常用文件操作以及文件下载功能,文件操作包括打开文件夹和打开各类的文件(apk、avi...

    android 内存 文件读写操作

    android 通过Environment环境 获取手机根目录 读写 文件操作。

    android xml文件操作

    android xml文件操作,txt文件操作,md5加密,百度地图两点之间距离计算

    Android文件操作

    Android Java 文件 Android文件操作

    android文件操作分装类

    android文件操作封装的java类,包括根据关键字查找文件,遍历目录,删除目录所有文件等等

    Android的文件存储

    Android的文件操作跟Java是一样的,文件操作分为输入流(InputStream)和输出流(OutputStream),输入流用于获取文件数据,输出流用于存储文件。 详细介绍请参考博文:...

    android读取文件内容操作.pdf

    android读取文件内容操作.pdf

    Android进行文件操作的代码例子

    Android进行文件操作的代码例子。用于演示文本文件读写、图片文件读写、Excel文件读写的功能。

    android txt文件保存读取操作

    android 写了一个工具类实现txt文件保存和读取

    android文件基本操作

    android系统文件的对基本操作,记得好像是用的测试类吧。。

    android文件操作

    这是一个android文件操作的实例,包括文件的读写与删除

    android对文件和sd卡的各种操作

    android对文件和sd卡的各种操作,代码注释相当明确,大家可以用于项目当中

Global site tag (gtag.js) - Google Analytics