android 读写txt文件
来源:互联网 发布:网络女神排行 编辑:程序博客网 时间:2024/05/17 01:16
写了两个函数,发现调用时老是异常,原来是openFileOutput 方法必须在Activity中调用。所以一下两个函数都必须在Activity中调用。
public class FileStream{
private Context context;
public FileStream(Context context) { this.context = context;}/** * *@Title:SaveFile *@param fileName 需要加后缀,如.txt *@param fileContent *@throws IOException *@Description: 保存文件 */public void SaveFileTxt(String fileName, String fileContent) { try{ FileOutputStream fout =context.openFileOutput(fileName,Context.MODE_PRIVATE); byte [] bytes = fileContent.getBytes(); fout.write(bytes); fout.close(); } catch(Exception e){ e.printStackTrace(); } }/** * *@Title:ReadFileTxt *@param fileName *@return *@throws IOException *@Description: 读txt文档,返回读出来的数 */public String ReadFileTxt(String fileName) throws IOException{ String res=""; try{ FileInputStream fin = context.openFileInput(fileName); int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); } catch(Exception e){ e.printStackTrace(); } return res; }
}
0 0
- Android TXT文件读写
- Android txt文件读写
- Android TXT文件读写
- Android TXT文件读写
- android 读写txt文件
- Android TXT文件读写
- (Android)Android TXT文件读写
- android txt文件的读写总结
- Java Android 创建读写TXT文件
- Android读写SD卡中的txt文件
- VB读写TXT文件
- txt文件读写
- C#读写txt文件
- Java读写txt文件
- txt文件读写
- .Txt 文件读写
- DataGrid读写txt文件
- javascript 读写txt文件
- leetcode--Swap Nodes in Pairs
- SqlServer 2008 R2 域环境镜像问题小结
- 微信登陆debug 需要改变keystore
- EJB的存根和骨架的工作原理
- nyoj 171 聪明的kk
- android 读写txt文件
- 自定义带Button的通知,并实现对Button的监听
- 怎么使用class使用hibernate的插入数据到数据库
- ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等。 设置的方式包括: 1. 在layout xml中定义Android:s
- Data Binding Library(数据绑定库)
- Android Accessibility学习
- C - Just a Hook(写的第一道线段树区间更新的题目,卡了半天)
- 【工具篇】
- Maven学习总结(三)——使用Maven构建项目