针对文件的存取

 package com.example.file01;

 import com.example.service.FileService;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.*;
 import android.widget.*;

 public class MainActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         Button button=(Button)this.findViewById(R.id.button1);

         button.setOnClickListener(new ButtonClickListener());

     }

     private class ButtonClickListener implements View.OnClickListener{

         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub
             EditText filename1=(EditText)findViewById(R.id.editText1);
             EditText content1=(EditText)findViewById(R.id.EditText01);
             String filename=filename1.getText().toString();
             String content=content1.getText().toString();
             FileService service=new FileService(getApplicationContext());
             try {
                 service.save(filename,content);

                 Toast.makeText(getApplicationContext(), "success", 1).show();
             } catch (Exception e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();

                 Toast.makeText(getApplicationContext(), "fail", 1).show();
             }

         }

     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
         if (id == R.id.action_settings) {
             return true;
         }
         return super.onOptionsItemSelected(item);
     }
 }
 package com.example.service;

 import java.io.ByteArrayOutputStream;
 import java.io.*;
 import java.io.FileOutputStream;

 import android.content.Context;

 public class FileService {
     private Context context;

     public FileService(Context context) {
         this.context = context;
     }

     /**
      * 保存文件
      * @param filename    文件名
      * @param content    文件内容
      */
     public void save(String filename, String content)throws Exception {
         // TODO Auto-generated method stub
         FileOutputStream outStream=context.openFileOutput(filename, Context.MODE_PRIVATE);

         outStream.write(content.getBytes());
         outStream.close();
     }

     /**
      *
      * @param filename
      * @return
      * @throws Exception
      */
     public String read(String filename)throws Exception {
         FileInputStream inStream=context.openFileInput(filename);
         ByteArrayOutputStream outStream=new ByteArrayOutputStream();
         byte[]    buffer =new byte [1024];
         int len=0;
         while((len=inStream.read(buffer))!=-1){
             outStream.write(buffer,0,len);

         }
         byte[] date=outStream.toByteArray();
         return new String(date);

     }
 }

测试,需要添加一些东西

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.file01"
     android:versionCode="1"
     android:versionName="1.0" >

     <uses-sdk
         android:minSdkVersion="19"
         android:targetSdkVersion="19" />

     <application

         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <uses-library android:name="android.test.runner" />
         <activity
             android:name=".MainActivity"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>

     </application>

         <instrumentation
         android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.example.file01" />
 </manifest>

测试用代码

 package com.example.test;

 import com.example.service.FileService;

 import android.test.AndroidTestCase;
 import android.util.Log;

 public class testfileread extends AndroidTestCase {
     private static final String TAG="wang.txt";
         public void testread() throws Throwable{
             FileService service =new FileService(this.getContext());
             String s=service.read("wang.txt");
             Log.i(TAG, s);
         }
 }

Android 笔记 文件存取 day5的更多相关文章

  1. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  2. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  3. Python学习笔记,day5

    Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...

  4. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  5. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  6. 《从零开始学Swift》学习笔记(Day5)——我所知道的标识符和关键字

    Swift 2.0学习笔记(Day5)——我所知道的标识符和关键字   原创文章,欢迎转载.转载请注明:关东升的博客 好多计算机语言都有标识符和关键字,一直没有好好的总结,就是这样的用着,现在小小的整 ...

  7. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  8. Android笔记--View绘制流程源码分析(二)

    Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...

  9. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

随机推荐

  1. coreseek安装

    一.  Sphinx简介 Sphinx是由俄罗斯人Andrew Aksyonoff开发的一个全文检索引擎.意图为其他应用提供高速.低空间占用.高结果 相关度的全文搜索功能.Sphinx可以非常容易的与 ...

  2. 使用django开发博客过程记录1——数据库设计

    1.数据库设计 2.插入测试数据 3.配置相关问题 1.数据库设计 数据库有简单的三张表:Article.Category.Tag以下是代码 # -*- coding:utf-8 -*- from _ ...

  3. 前端Js框架汇总

    概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人心,.net co ...

  4. 三种执行SQL语句的的JAVA代码

    问题描述: 连接数据库,执行SQL语句是必不可少的,下面给出了三种执行不通SQL语句的方法. 1.简单的Statement执行SQL语句.有SQL注入,一般不使用. public static voi ...

  5. Jquery 插件\Js 插件收集

    1.linq.js 使得js 的数组像c#的linq操作一下样 http://linqjs.codeplex.com/ 2.分页插件 https://github.com/mricle/Mricode ...

  6. XUnit - Shared Context between Tests

    原文 单元测试类通常都会有share setup和cleanup的相关代码.xUnit.net根据共享的范围提供了几种share setup和cleanup的方法. Constructor and D ...

  7. Xcode 快捷键、常用技巧

    关于iOS开发中的技能快捷键 经常使用鼠标太TM的D疼了,快捷键能大大地提高我们的开发速度,使我们的手指尽情的在键盘上飞舞,优美的代码,哈哈哈,那些常规的复制.粘贴.剪切请自行度娘或者Google一下 ...

  8. linux磁盘空间查询

    LINUX服务器查询 1. du -sch *   使用该命令查询当前目录下文件夹占用的空间的情况 2. df -hl  查询磁盘剩余空间 3. root权限  fdisk -l

  9. C#RSA算法实现+如何将公钥为XML格式转为PEM格式,给object-C使用

    .net中,处于安全的考虑,RSACryptoServiceProvider类,解密时只有同时拥有公钥和私钥才可以.原因是公钥是公开的,会被多人持有.这样的数据传输是不安全的.C#RSA私钥加密,公钥 ...

  10. 面localStorage用作数据缓存的简易封装

    面localStorage用作数据缓存的简易封装 最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但 ...