Android 文件操作
2015-08-03 20:28
573 查看
Android 文件操作
Android集成了Java.IO库,所有java中基于“流”的输入输出类也可以在这里运用。这里也展示一个简答的小例子。
fileService.java
package com.file.save2sdcard; 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; /** * 文件操作服務類 * @author QT * */ public class fileService { private Context context; public fileService(Context context) { this.context = context; } public fileService() { } public static String readFileFromSdcard(String fileName) { String message = ""; FileInputStream fileInputStream = null; // 字節數組輸出流,寫到内存中,不需要關閉 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); //文件 File file = new File(Environment.getExternalStorageDirectory(), fileName); try { //对于外置存储卡进行读写操作,首先要确定状态 if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { fileInputStream = new FileInputStream(file); int len = 0; //缓冲数组 byte[] data = new byte[1024]; while ((len = fileInputStream.read(data)) != -1) { byteOutputStream.write(data, 0, len); } message = byteOutputStream.toString(); //messge=new String(byteOutputStream.toByteArray()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return message; } /** * 保存文件到sd卡 * * @param fileName * 文件名稱 * @param content * 内容 * @return 文件是否保存的標志 */ public static boolean writefileToSdcard(String fileName, String content) { boolean flag = false; FileOutputStream fileOutputStream = null; // 设置文件路径和文件名 File file = new File(Environment.getExternalStorageDirectory(), fileName); // 如果外置存储卡处于可用状态 if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { try { fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); flag = true; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); fileOutputStream = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return flag; } }
FileServiceText.java 测试类
package com.file.save2sdcard; import android.content.Context; import android.test.AndroidTestCase; import android.util.Log; /** * 文件操作測試類 * @author QT * */ public class FileServiceText extends AndroidTestCase { public static String TAG="FileServiceText"; public FileServiceText() { // TODO Auto-generated constructor stub } public void TestSaveFile(){ Context context=getContext(); fileService files=new fileService(context); boolean flag=files.writefileToSdcard("hello.txt", "看不見你的笑,我怎麽睡的着"); Log.i(TAG, "---->>"+flag); } public void TestReadFile(){ Context context=getContext(); fileService files=new fileService(context); String message=files.readFileFromSdcard("hello.txt"); Log.i(TAG, "---->>"+message); } }
AndroidManifest.xml 添加权限和单元测试支持
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.file.save2sdcard" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" /> <!-- 添加读取外置存储卡的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 添加andorid单元测试 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.file.save2sdcard" > </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.file.save2sdcard.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>
运行结果
向SDCard保存数据
从SDCard得到数据
相关文章推荐
- Android OpenGL ES 2.0之混合与雾的学习
- Android的多项选择设计
- Android 跳转基础应用(联系人,日历)
- <转载>Android 工程报错解决 Unable to resolve target 'android-17'
- Android showmap
- Android Api Demos登顶之路(十七)Redirection
- android-support-v7包的添加
- javafx 运行在android上遇到的难题-完美解决
- Android ViewFlipper用法浅析
- Android视频播放之VideoView
- Android 毛玻璃效果
- Android环境搭建
- Android 旋转动画,停止和持续旋转
- MAC OS ANDROID环境搭建
- android中如何实现mvc,用MVC进行重构
- Android Handler消息处理顺序分析
- Android Handler消息处理顺序分析
- Android Handler消息处理顺序分析
- Android procrank
- Android创建菜单Menu