加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

android中文件操作

发布时间:2020-12-14 23:50:14 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 android开发中涉及到的文件操作主要是往sd卡中或是内在中读取文件数据,大概的操作如下: public class FileOperator { private Context context; //

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

android开发中涉及到的文件操作主要是往sd卡中或是内在中读取文件数据,大概的操作如下:
public class FileOperator {  
    private Context context; //上下文 
      public FileOperator(Context c) {  
        this.context = c;  
    }  
  
  //首先是个公用的方法:
  private String dealStream(InputStream is) throws IOException {  
        int buffersize = is.available();// 取得输入流的字节长度  
        byte buffer[] = new byte[buffersize];  
        is.read(buffer);// 数据读入数组  
        is.close();// 读取完毕关闭流。  
        String result = EncodingUtils.getString(buffer,"UTF-8");// 防止乱码  
        return result;  
    }  

    // 读取sd中的存在的文件  
    public String readSDCardFile(String path) throws IOException {  
        File file = new File(path);  
        FileInputStream fis = new FileInputStream(file);  
        String resStr = dealStream(fis);  
        return resStr ;  
    }  
  
    // res目录下新建raw资源文件夹,只能读不能写入  
    public String readRawFile(int fileId) throws IOException {  
        // 取得输入流  
        InputStream is = context.getResources().openRawResource(fileId);  
        String resultStr = dealStream(is);// 返回一个字符串  
        return resultStr;  
    }  
   
    // 在assets下的文件,只能读取不能写入  
    public String readAssetsFile(String filename) throws IOException {  
        // 取得输入流  
        InputStream is = context.getResources().getAssets().open(filename);  
        String resStr = dealStream(is);// 返回一个字符串  
        return resStr;  
    }  
  
    // 往sd卡中写入文件  
    public void writeToSDCard(String path,byte[] buffer) throws IOException {  
        File file = new File(path);  
        FileOutputStream fos = new FileOutputStream(file);  
        fos.write(buffer);// 写入buffer数组。如果想写入一些简单的字符,可以将String.getBytes()再写入文件;  
        fos.close();  
    }  
  
    // 将文件写入应用的data/data的files目录下  
    public void writeToDateFile(String fileName,byte[] buffer) throws Exception {  
        byte[] buf = fileName.getBytes("iso8859-1");  
        fileName = new String(buf,"utf-8");  
        FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_APPEND);// 打开模式,有几种,此处表示添加在文件后面  
        fos.write(buffer);  
        fos.close();  
    }  
  
    // 读取应用的data/data的files目录下文件数据  
    public String readDateFile(String fileName) throws Exception {  
        FileInputStream fis = context.openFileInput(fileName);  
        String resultStr = dealStream(fis);// 返回一个字符串  
        return resultStr;  
    }  
}  

最后我们不要忘记在清单文件AndroidManfiest.xml中加上sd卡操作权限:
<uses-permission?android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"?/>?//在SDCard中创建与删除文件权限
<uses-permission?android:name="android.permission.WRITE_EXTERNAL_STORAGE"?/>? //往SDCard写入数据权限

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读