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 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...
随机推荐
- ACM——数的计数
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1050 数的计数 时间限制(普通/Jav ...
- SQL Server调优系列基础篇 - 并行运算总结(二)
前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...
- 网站部署后,ie不能显示本地的图片
html:<div id="imgPreview" style='width:144px; height:80px;'> ...
- JS自动合并表格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Object-C在Nil上调用方法
在Object-C中,nil对象的作用等同于很多其它语言的NULL指针.不同的地方在于,在nil上调用方法不会导致程序崩溃或抛出异常. 这种技术被用在很多地方,但是对于我们来讲,最主要的就是我们不用在 ...
- Mac 下显示隐藏文件
将下面的命令粘贴进终端,按提示操作即可(可能需要输入电脑密码) 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:d ...
- docker私有仓库
1.docker pull registry 2.sudo docker run -d -p 5000:5000 registry 默认情况下,会将仓库存放于容器内的/tmp/registry目录下, ...
- 构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml
学习maven项目时 搭建个ssm项目 算是给自己留个备份吧 环境说明: MyEclipse10 Maven 3.2.3 框架: struts2 2.3.24.1 spring3 3. ...
- 一个关于hightcharts的x轴刻度问题
最近做公司的一个报表系统,需要折线图,圆柱图形等来进行统计,经过最后考察,决定用当下较为流行的Highchart插件来进行实现,highchart用起来非常方便,只用对后台传过来的数据进行简单的处理后 ...
- H5小内容(二)
音视频处理 视频处理 基本内容 使用Flash技术处理HTML页面中的视频内容 包含音频.动画.网页游戏等 特点 浏览器原生不支持(IE浏览器要求安装A ...