今天复习一下文件读取
来源:互联网 发布:统赢编程教程 编辑:程序博客网 时间:2024/06/05 21:07
自己整理写的一个文件读取类,可以读取和写入
public class FileService { public Context context; public FileService(Context context) { this.context = context; } public void saveToSDCard(String filename,String content){ File file = new File(Environment.getExternalStorageDirectory(),filename); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public void save(String filename,String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = context.openFileOutput(filename,Context.MODE_PRIVATE); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveAppend(String filename,String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = context.openFileOutput(filename,Context.MODE_APPEND); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveWriteable(String filename,String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = context.openFileOutput(filename,Context.MODE_WORLD_WRITEABLE); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveReadable(String filename,String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = context.openFileOutput(filename,Context.MODE_WORLD_READABLE); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveRW(String filename,String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = context.openFileOutput(filename,Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public String read(String filename){ FileInputStream fileInputStream = null; try { fileInputStream = context.openFileInput(filename); } catch (FileNotFoundException e) { e.printStackTrace(); } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte byteArray[] = new byte[1024]; int len = 0; try { while((len = fileInputStream.read(byteArray))!=-1){ byteArrayOutputStream.write(byteArray,0,len); } } catch (IOException e) { e.printStackTrace(); } String content = new String(byteArray); return content; }}
0 0
- 今天复习一下文件读取
- 今天复习了一下java的基础知识。
- 今天复习了一下Java基础
- 好好看看Java 复习一下。今天复习的内容
- qt4 designer今天有重新复习了一下~
- Android复习笔记(4) - 文件读取相关
- 读取.properties属性文件--今天的郁闷
- 今天学习数据结构,复习一下学习已久的结构体
- 今天刚刚认真区别了一下两个文件读写方式
- Android学习之路-------复习一下缓存和读取缓存,网络加载图片
- 复习一下光照模型
- 复习一下log4j
- 复习一下VC
- 复习一下范式
- 复习一下sqlplus 命令
- 复习一下RSA
- Servlet生命周期(复习一下)
- 复习一下C指针
- 解决ImageView上下有空白的办法
- BZOJ 1600: [Usaco2008 Oct]建造栅栏
- Java线程:创建与启动
- vim常用配置
- 南邮-2045(最大子序列和的问题)
- 今天复习一下文件读取
- 机器学习笔记
- NSTimer定时器
- vi的使用
- 沙盒目录
- linux 下 shell 截取字符串的方法总结
- Django Bootstrap toolkit
- 线程的生产者消费者复习
- ACM:入口的选择------深度优先搜索