Android 写文件到手机
1)// 在手机中创建文件
FileOutputStream phone_outStream =this.openFileOutput("1.txt", Context.MODE_PRIVATE);
phone_outStream.write("HELLO".getBytes());
FileOutputStream phone_outStream1 =openFileOutput("1.txt", Context.MODE_APPEND); //追加模式继续写
phone_outStream1.write(" world".getBytes());
//读取并显示
byte[] s= new byte[80];
FileInputStream phone_inputStream =openFileInput("1.txt");
phone_inputStream.read(s);
Toast.makeText(this, new String(s), Toast.LENGTH_SHORT).show();
结论:不管手机data文件夹是否能在DDMS中看到东西, (没有root权限就会空空如也.)程序能够正常运行,并toast出正确内容.
2)如果试图用该方法来写入到手机内部存储中,是不行的:
java.io.FileNotFoundException: /2.txt: open failed: EROFS (Read-only file system)
File f = new File("2.txt");
FileOutputStream fs = new FileOutputStream( f ); //这句导致异常
openFileOutput();是android的api, android.content.ContextWrapper.openFileOutput();
FileOutputStream .是Java的类. java.io.FileOutputStream
此文转自:http://www.cnblogs.com/sinawear/archive/2012/11/26.html
==============================================
public static void writeStringAsFile(final String fileContents, String fileName) {
Context context = App.instance.getApplicationContext();
try {
FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));
out.write(fileContents);
out.close();
} catch (IOException e) {
Logger.logError(TAG, e);
}
}
public static String readFileAsString(String fileName) {
Context context = App.instance.getApplicationContext();
StringBuilder stringBuilder = new StringBuilder();
String line;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
while ((line = in.readLine()) != null) stringBuilder.append(line);
} catch (FileNotFoundException e) {
Logger.logError(TAG, e);
} catch (IOException e) {
Logger.logError(TAG, e);
}
return stringBuilder.toString();
}
Android 写文件到手机的更多相关文章
- android 写文件到sd卡问题小记
android 写文件到sd卡问题小记 事情是这样子的.... 这天我开始编写项目调试工具,高大上不?-----其实就是记录实时网络请求和崩溃日志相关等的小工具(此处一个会心的微笑). 然后我是这样写 ...
- android 写文件权限
首先,在manifest.xml中添加user permission:<uses-permission android:name="android.permission.WRITE_E ...
- [android] 保存文件到手机内存
/*****************2016年5月4日 更新*******************************/ 知乎:Android 没有沙盒保护机制吗,WhatsApp 信息为何可被随 ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- 封装一个帮助类来写文件到android外置存储器上
项目地址:点击打开 项目简介:写文件到android外置存储器的一个帮助类,和它的demo程序 它是如何工作的呢? 1.创建 AppExternalFileWriter 对象并传递context(上下 ...
- android将对象序列化到文件:直接写文件与用Serializable接口的对比
1.用文件读写1024个对象的日志 10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 102 ...
- Android 读取和保存文件(手机内置存储器)
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- android之写文件到sd卡
1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- 【转】android 安卓APP获取手机设备信息和手机号码的代码示例
http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...
随机推荐
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
- sql server 的JDBC 配置
1.java中使用数据库需要用JDBC(java database connection)来连接和操作 下载地址:http://www.microsoft.com/en-us/download/det ...
- 1066. Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child su ...
- 10.21_Nutz批量插入顺序,POI,wiki持续关注,POI,SSH,数据库优先
(1)Nutz,dao的批量插入会关注顺序吗? http://www.douban.com/group/topic/64322582/ (2)工作需要优先!!! POI,SSH,数据库管理及plsq ...
- mysql 5.7安装脚本
[root@HE2 ~]# cat mysql_auto_install.sh ###### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可########## ...
- nginx 限制及指定IP或IP段访问
nginx 限制及指定IP或IP段访问. location / { deny 192.168.1.1; allow ; allow ; deny all; } 企业问题案例:Nginx做反向代理的时候 ...
- nginx 显示discuz页面
我也不知道我题目表述的对不对.我也是刚学习. 安装nginx 显示discuz的界面.我看了好几次视频,也没发现哪的问题.就是一直出现502的错误代码 我又重新查了几次.觉得需要注意一下几点: 1,关 ...
- apache config directive – order, allow, deny
在对apache进行配置的时候,常看到oerder, allow, deny.现在就简单回顾一下其用法. 对于每个对于资源的请求,服务器可以配置是否允许这个请求通过.在配置当中,使用的是允许与不允许的 ...
- linux ptheard 生产者消费者
; { { printf( pthread_mutex_lock(&mutex); != g_iBufSiz ...
- ubuntu 初始
1.命令行界面与图形界面 ctrl + alt + f1进入命令行界面 ctrl + alt + f7 切换图形界面 2.ubuntu 的wubi安装与卸载 第一:在win 系统下启动DOS,进入命令 ...