android / java 字节流写入文件
来源:互联网 发布:淘宝买家怎么修改评论 编辑:程序博客网 时间:2024/05/19 05:40
将服务器传来的字节流写入文件
filePath 为写入路径
fileName 为文件名(包括后缀)
注: android 64 包为 android.util.Base64
java也差不多
private void initData(String data,String name) { String filePath = "/sdcard/IntegrateMonitorFile/"; String fileName = name; Log.d("wocao", Realfile); byte[] bytes = Base64.decode(data, Base64.DEFAULT); writeTxtToFile(bytes, filePath, fileName); } // 将字符串写入到文本文件中 public void writeTxtToFile(byte[] bytes, String filePath, String fileName) { //生成文件夹之后,再生成文件,不然会出错 makeFilePath(filePath, fileName); String strFilePath = filePath+fileName; try { /* File file = new File(strFilePath); if (!file.exists()) { Log.d("wocao", "Create the file:" + strFilePath); file.getParentFile().mkdirs(); file.createNewFile(); }else{ file.delete(); file.getParentFile().mkdirs(); file.createNewFile(); }*/ /* RandomAccessFile raf = new RandomAccessFile(file, "rwd"); raf.seek(0); raf.write(bytes); raf.close();*/ FileOutputStream out = new FileOutputStream(strFilePath); out.write(bytes); out.close(); } catch (Exception e) { Log.e("wocao", "Error on write File:" + e); } } // 生成文件 public void makeFilePath(String filePath, String fileName) { File file = null; makeRootDirectory(filePath); try { file = new File(filePath + fileName); if (!file.exists()) { file.createNewFile(); }else{ /* file.delete(); file.createNewFile();*/ Message message = new Message(); message.what = 3; mHandler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } return ; } public int makeFilePath( String fileName) { File file = null; String filePath = "/sdcard/IntegrateMonitorFile/"; makeRootDirectory(filePath); try { file = new File(filePath + fileName); if (!file.exists()) { file.createNewFile(); }else{ /* file.delete(); file.createNewFile();*/ Message message = new Message(); message.what = 3; mHandler.sendMessage(message); return 1; } } catch (Exception e) { e.printStackTrace(); } return 0; } // 生成文件夹 public void makeRootDirectory(String filePath) { File file = null; try { file = new File(filePath); if (!file.exists()) { file.mkdir(); } } catch (Exception e) { Log.i("wocao:", e+""); } }}
阅读全文
0 0
- android / java 字节流写入文件
- 文件写入字节流
- 通过java字节流复制文件,逐个字节读取写入
- java缓冲字节流复制文件,逐个字节读取、写入
- java(147)字节流文件读取,写入,追加文件
- Java之基于字节流文件的写入和读取
- java把文件写入内存---输出字节流
- Java-文件读取写入整型(字节流)
- java 使用IO字节流将一句话写入文件
- java 字节流写入中文
- 字节流读取、写入、复制文件
- java IO流字节流写入大写字母
- Java 字节流 读取和写入
- java字节流的方式复制一个文件,按数组读取和写入
- java中的文件操作:读取写入byte[]字节流、string字符串、list列表
- java向文件中写入内容,字节流,字符流,缓冲,复制文件,设置字符编码 实例
- java io流 写入文件
- java字节流和字符流操作文件,指定编码写入和读取,递归创建上层目录
- (牛客网)栈的压入、弹出序列
- Python安装和PyCharm(2017.1.3)安装、注册、汉化教程
- js 模拟表单post方式提交
- PhpExcel导出excel 配置缓存
- springMVC常用注解标签详解
- android / java 字节流写入文件
- 设计模式系列(10)工厂模式
- java.lang.reflect.UndeclaredThrowableException原因和解决方法
- 42. Trapping Rain Water
- 【论文阅读】Convolutional Sequence to Sequence Learning (未完待续)
- App提交苹果审核被拒原因总结
- 微信公众号内使用模板功能发送信息
- JQuery图表插件Flot之属性方法
- 安卓开发之Kotlin