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 写文件到sd卡问题小记
android 写文件到sd卡问题小记 事情是这样子的.... 这天我开始编写项目调试工具,高大上不?-----其实就是记录实时网络请求和崩溃日志相关等的小工具(此处一个会心的微笑). 然后我是这样写 ...
android 写文件权限
首先,在manifest.xml中添加user permission:
android之写文件到sd卡
1.main.xml <?xml version="1.0" encoding="utf-8"?>
【转】android 安卓APP获取手机设备信息和手机号码的代码示例
http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...
随机推荐
pivotx的entry和page内容里的日期格式修改
欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=63 1. 文章发布时间的确定 如果服务器空间在国内还好说, 如果在国外的话, 文章编辑时 ...
mongo日志切割脚本
两种mongo日志切割脚本 vim /etc/logrotate.d/mongodb /home/mongodb/mongolog/mongod.log { daily rotate 7 compre ...
CV界的明星人物们
CV界的明星人物们 来自:http://blog.csdn.net/necrazy/article/details/9380151,另外根据自己关注的地方,加了点东西. 今天在cvchina论坛上看到 ...
java:String使用equals和==比较的区别
原文链接:http://www.cnblogs.com/tinyphp/p/3768214.html "=="操作符的作用 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的 ...
ubuntu14.04配置Hive1.2.1
1.添加环境变量:vi ~/.bashrc #HIVE VARIABLES START export HIVE_HOME=/usr/local/hive-1.2.1 export PATH=$PATH ...
【项目1-1】使用HTML5+CSS3绘制HTML5的logo
作为一个WEB小萌新,自学了有一段时间,总是感觉停滞不前.最近反思中,想到前贤一句话:书读百遍其义自见.说到底,还是项目做的少,如果做多了,想必自然会得心应手. 利用HTML5+CSS3绘制HTML5 ...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
关于Spring中的<;context:annotation-config/>;配置(开启注解)
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...
【代码笔记】Web-HTML-布局
一, 效果图. 二,代码.
...SRTP讨论
1.接下来任务如何分工? 2.接下来要完成哪些工作? 硬件上要完成哪些? 1)环境数据的采集(我) 2)对小车的控制(我) 3)路径规划(喻) 4)小车的避障(雷) 5)环境数据的处理融合(我) 6) ...