Android TXT文件读写
- package com.wirelessqa.helper;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import org.apache.http.util.EncodingUtils;
- import android.app.Activity;
- public class FileAccess extends Activity {
- /**
- * 一、私有文件夹下的文件存取(/data/data/包名/files)
- *
- * @param fileName
- * @param message
- */
- public void writeFileData(String fileName, String message) {
- try {
- FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
- byte[] bytes = message.getBytes();
- fout.write(bytes);
- fout.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * //读文件在./data/data/包名/files/下面
- *
- * @param fileName
- * @return
- */
- public String readFileData(String fileName) {
- String res = "";
- try {
- FileInputStream fin = 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;
- }
- /**
- * 写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput
- * 不同点:openFileOutput是在raw里编译过的,FileOutputStream是任何文件都可以
- * @param fileName
- * @param message
- */
- // 写在/mnt/sdcard/目录下面的文件
- public void writeFileSdcard(String fileName, String message) {
- try {
- // FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
- FileOutputStream fout = new FileOutputStream(fileName);
- byte[] bytes = message.getBytes();
- fout.write(bytes);
- fout.close();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- // 读在/mnt/sdcard/目录下面的文件
- public String readFileSdcard(String fileName) {
- String res = "";
- try {
- FileInputStream fin = new FileInputStream(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;
- }
- /**
- * 二、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
- *
- * @param fileInRaw
- * @return
- */
- public String readFromRaw(int fileInRaw) {
- String res = "";
- try {
- InputStream in = getResources().openRawResource(fileInRaw);
- int length = in.available();
- byte[] buffer = new byte[length];
- in.read(buffer);
- res = EncodingUtils.getString(buffer, "GBK");
- // res = new String(buffer,"GBK");
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return res;
- }
- /**
- * 三、从asset中获取文件并读取数据(资源文件只能读不能写)
- *
- * @param fileName
- * @return
- */
- public String readFromAsset(String fileName) {
- String res = "";
- try {
- InputStream in = getResources().getAssets().open(fileName);
- int length = in.available();
- byte[] buffer = new byte[length];
- in.read(buffer);
- res = EncodingUtils.getString(buffer, "UTF-8");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return res;
- }
- }
Android TXT文件读写的更多相关文章
- python 简单的txt文件读写
1 读取txt文件.跟c相比,python的文件读写简直是方便的可怕 首先是读取文件 首先获得文件名称,然后通过 open函数打开文件,通过for循环逐行读出文件内容 #!python file by ...
- java 对txt文件读写(已经封装好)
读文件: public static String readTxt(String txtPath) { File file = new File(txtPath); if(file.isFile() ...
- Android sdcard文件读写操作
这次演示以,安卓原生操作系统 Nexus_6手机进行操作: AndroidManifest.xml配置相关权限: <!-- 增加权限 --> <uses-permission and ...
- Android开发 文件读写openFileOutput与openFileInput
package com.example.androidtest; import java.io.ByteArrayOutputStream; import java.io.FileInputStrea ...
- C#txt文件读写基本操作
string strFileName=@"C:\Users\Administrator\Desktop\记事2.txt"; //判断是否存在 if (File.Exists(str ...
- C# txt文件读写
//读取文件内容 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出. ]; ]; public void Read() { try { FileStream fil ...
- python txt文件读写(追加、覆盖)
(1)在lucky.txt中新增内容(覆盖:每次运行都会重新写入内容) f = "lucky.txt" a =8 with open(f,"w") as fil ...
- Java逐行读写TXT文件
package help; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...
- cv.Mat 与 .txt 文件数据的读写操作
1.按OpenCV格式实现的 .txt 文件读写 可以用 cvSave 和 cvLoad 实现,格式和 .xml/.yml 的差不多,不过如果专用与 OpenCV 的数据读写,还是用 .xml/.y ...
随机推荐
- LayoutInflater 与 inflate
Instantiates a layout XML file into itscorresponding View objects. LayoutInflater作用是将layout的xml布局文件实 ...
- USB2.0的基本学习
SB2.0是在1.0的基础上于2000年提出来的,在1.0的基础上曾加了480Mbps的数据传输率.USB2.0具有以下的优点: 1.每个USB系统中有一个主机,通过级联的方式连接多个外部设备,最多可 ...
- C语言malloc和free实现原理
以下是一段简单的C代码,malloc和free到底做了什么? int main() { char* p = (char*)malloc(32); free(p); return 0; } malloc ...
- ecshop的模板文件中如何判断用户是否登录
ecshop中对于smarty的运用和改造有很大的值得借鉴的地方,在dwt模板文件中可以直接判断用户是否登录,现在有规定,凡是只展示不销售的电商平台,一律不得展示商品价格,但可以在用户登录后显示. & ...
- linux学习(二)-目录的操作命令
Linux命令大全:http://www.jb51.net/linux/ 目录分绝对路径和相对路径 : 绝对路径,在路径前会加 / 相对路径就是相对于当前的路径,直接 路径名即可. 查看目录: cd ...
- STL的移动算法
要在自己定义类型中使用移动算法.须要在元素中提供移动赋值运算符.移动赋值运算符和std::move()详见<c++高级编程>第9章 class mystring { public: str ...
- 一个很好的用C#导出数据到Excel模板的方法
/// <summary> /// 导数据到Excel模板 /// </summary> /// <param name="tab">要输出内容 ...
- Virtualbox 启动虚拟机报错以及扩展、显卡驱动安装
一.Virtualbox虚拟机启动报错,如图 预先估计是BIOS中的cpu Virtualtion虚拟化支持是disable,结果一看是enable. 接下来只好Google,找到了这么一个帖子:ht ...
- hadoop 学习
不同版本间Hadoop拷贝 通过NFS,将hdfs挂在到本地
- (转载)SQL语句中Group by语句的详细介绍
转自:http://blog.163.com/yuer_d/blog/static/76761152201010203719835 SQL语句中Group by语句的详细介绍 ...