android配置文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.androidsdk"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="10" />    <!--添加读写sdcard的授权  -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <!--测试 -->    <instrumentation android:name="android.test.InstrumentationTestRunner"         android:targetPackage="com.example.androidsdk">            </instrumentation>    <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="com.example.androidsdk.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></manifest>

下面是读写的方法类

package com.example.androidsdk;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.content.Context;import android.os.Environment;import android.util.Log;public class FileService {private Context context;public FileService(Context context){this.context=context;}public FileService(){}public boolean saveContentToSdcard(String fileName,String content){boolean flag=false;FileOutputStream fileOutputStream=null;//获取sdcard卡所在的路径File file=new File(Environment.getExternalStorageDirectory(), fileName);//判断sdcard卡是否可用    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){        try {fileOutputStream=new FileOutputStream(file);    fileOutputStream.write(content.getBytes());    flag=true;    } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}        }return flag;}//读取public String getFileFromSdcard(String filename){FileInputStream inputStream=null;//缓存的流,和磁盘无关,不需要关闭ByteArrayOutputStream outputStream=new ByteArrayOutputStream();File file=new File(Environment.getExternalStorageDirectory(),filename);if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){try {inputStream=new FileInputStream(file);    int len=0;    byte[] data=new byte[1024];    while((len=inputStream.read(data))!=-1){}{    outputStream.write(data,0,len);    }    } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(inputStream!=null){try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return new String(outputStream.toByteArray());}}

测试方法

package com.example.androidsdk;import android.content.Context;import android.test.AndroidTestCase;import android.util.Log;public class MyTest extends AndroidTestCase {   private final String TAG="MyTest";  public void saveFile(){  Context context=getContext();FileService fileService=new FileService(context);boolean flag=fileService.saveContentToSdcard("hello.txt", "111张航");Log.i(TAG,"-->>"+flag);}public void readFile(){Context context=getContext();FileService fileService=new FileService(context);String read=fileService.getFileFromSdcard("hello.txt");Log.i(TAG, "-->>"+read);}}


更多相关文章

  1. android Handler使用
  2. android ListView 动态加载
  3. update android api
  4. 【Android】自定义 Tabhost
  5. android SwitchPreference ListPreference使用
  6. Android(安卓)发送短信
  7. android动态添加ImageView注意
  8. xpose框架使用android studio
  9. android的数据库API操作

随机推荐

  1. 在mac 上配置AndroidStudio碰到的坑
  2. android HAL介绍
  3. Android(安卓)创建与解析XML(二)—— Dom方
  4. Android:Android学习路线图
  5. Android 各种实现Tab效果的实现方式
  6. Android系统移植与调试之------->如何修
  7. Android ART 垃圾回收机制
  8. Android 开发笔记 —— AndroidStudio 中
  9. android的一些开源项目
  10. Android(安卓)开发绕不过的坑:你的 Bitmap