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 写文件到手机的更多相关文章

  1. android 写文件到sd卡问题小记

    android 写文件到sd卡问题小记 事情是这样子的.... 这天我开始编写项目调试工具,高大上不?-----其实就是记录实时网络请求和崩溃日志相关等的小工具(此处一个会心的微笑). 然后我是这样写 ...

  2. android 写文件权限

    首先,在manifest.xml中添加user permission:<uses-permission android:name="android.permission.WRITE_E ...

  3. [android] 保存文件到手机内存

    /*****************2016年5月4日 更新*******************************/ 知乎:Android 没有沙盒保护机制吗,WhatsApp 信息为何可被随 ...

  4. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  5. 封装一个帮助类来写文件到android外置存储器上

    项目地址:点击打开 项目简介:写文件到android外置存储器的一个帮助类,和它的demo程序 它是如何工作的呢? 1.创建 AppExternalFileWriter 对象并传递context(上下 ...

  6. android将对象序列化到文件:直接写文件与用Serializable接口的对比

    1.用文件读写1024个对象的日志 10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 102 ...

  7. Android 读取和保存文件(手机内置存储器)

    1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...

  8. android之写文件到sd卡

    1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  9. 【转】android 安卓APP获取手机设备信息和手机号码的代码示例

    http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...

随机推荐

  1. 20160326 javaweb 请求转发和请求包含

    (1)请求转发: this.getServletContext().getRequestDispatcher("").forward(request,response); requ ...

  2. DLL使用总结

    最近项目中使用到了DLL,因此就把最近一段时间的学习总结一下,以备不时之需. 一.相关概念 1.动态链接库 自从微软推出第一个版本的Windows操作系统以来,动态链接库(DLL)一直是Windows ...

  3. JAVA多线程解惑之多线程返回值

    如果有人问题你,多线程可以有返回值吗?你怎么回答? 看下面例子,我定义了一个类实现了Callable 接口 public class MyCallable implements Callable< ...

  4. 时间处理总结(一).net

    不断整理中... 获取当前时间DateTime.Now.ToString("d") ;//获取到的日期格式为eg :2013/11/14 时间格式只取年月日DateTime.Now ...

  5. ios UIWebView截获html并修改便签内容

    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...

  6. c编程:提示用户输入一个0—9的数字进行猜测电脑产生的随机数。一共有三次机会。

    // //  main.c //  使用c语言进行编程: 题目:由电脑生成一个由0-9之间的随机数,提示用户也输入一个数字进行猜测.当猜测三次仍不中的时候结束程序. 编译环境:Xcode6.3 特别介 ...

  7. redis研究之watch

    具体的看注释,代码如下: public static void TransTest() { RedisClient client1 = new RedisClient(host, port); Red ...

  8. Ladder免费试用版

    Ladder这款vpn软件最近在play中国区排名非常高,节节高升,可见大家还真需要这东西,软件做的不错,大家可以在试用版中升级到正式版.   下载地址

  9. eclipse中定位引用的源码

    如图,在eclipse中,我想看BaseContoller是怎么实现的,将鼠标放上去,按住Ctrl单击左键就行了

  10. MySQL数据库原理

    我们知道,数据是信息的载体——一种我们约定了如何解释的符号.在计算机系统中,最常见的应该是文本数据.我们用它记录配置信息,写日志,等等.而在应用程序中,按一定的数据结构来组织数据的方式叫做数据库管理系 ...