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 ...
随机推荐
- Android studio gradle配置!!!【转】
转自:http://www.open-open.com/lib/view/open1415793464648.html Gradle 基本概念 首先我们学习几个gradle 的脚本语法,掌握了这几个语 ...
- IIS7性能优化:启用浏览器本地缓存
原文地址:http://jingyan.baidu.com/article/597035521b96e88fc00740af.html
- [11-1] adaboost DTree
main idea:用与$u_t$成正比的概率sampling生成的数据集$\widetilde{D}$训练DTree:用整个数据集$D$计算weighted$\epsilon_n$,计算$g_t$的 ...
- 安装vmware tools失败解决方法
失败提示: reating a new initrd boot image for the kernel. update-initramfs: Generating /boot/initrd.img- ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
- android 网络状态判断【转】
import java.net.InetAddress; import android.app.Activity;import android.content.Context;import andro ...
- varnish和squid的对比
Varnish与Squid的对比 说到Varnish,不能不提Squid,Squid是一个高性能的代理缓存服务器,它和varnish之间有诸多的异同点,这里分析如下: 下面是他们之间的相同点: ...
- 自定义配置文件的使用(web.config/app.config)
以下非原创作品,但都是自己看过理解并写过,记录下来,以便之后项目的使用或其它用途. (1)只需要简单配置单一属性值: <configuration> <configSections& ...
- poj2459 Treasure Exploration (闭包+二分)
这道题是让求派出机器人的最少数量,乍一看以为是简单的求最小路径覆盖,后来发现错了,因为有的点可以走多次,而二分中每个点只能走一次,所以要先用floyd进行传递闭包,然后用二分 #include< ...
- windows7 64位下运行 regsvr32 注册ocx或者dll的方法
来源:转载 it won't work for you unless you have some form of Visual Basic tools loaded on your system: ...