今天复习一下文件读取
2014-10-27 19:59
260 查看
自己整理写的一个文件读取类,可以读取和写入
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; } }
相关文章推荐
- 今天把PHP复习了一下.
- 好长时间没来这里了,今天复习一下java的东西,关于Date类和时间戳的转换
- 今天复习C++的const,又学到了很多内容,总结一下[原创]
- 今天复习了一下Java基础
- qt4 designer今天有重新复习了一下~
- 今天复习了一下完成端口网络模型
- 今天开始复习toefl,mark一下
- 今天复习了一下java的基础知识。
- 今天学习数据结构,复习一下学习已久的结构体
- 091001再来复习一下MFC程序的基础
- 今天陪女朋友逛街花了一块钱,好心疼啊!(转贴 大家开心一下!谁信,谁傻)
- 上学时的sql项目(模拟ATM),今日复习顺便共享一下
- 今天新学习的函数,和大家分享一下
- 今天一位同学测试了一下sizeof和strlen的区别
- 今天复习mysql明白了信息同步的问题
- 轻松使用线程 1 zt -- 今天又把这篇找出来看了一下
- C#中Dictionary和HashTable的相同点与不同点(今天又看到了HashSet,所以补充一下)
- 记录一下今天刚开通的博客
- 乱七八糟-今天看了一会儿NHibernate,调试记录一下
- 今天被领导好好教训了一番~特别总结一下