andriod学习——Content的openFileOutput 和 openFileInput操作文件
来源:互联网 发布:朱莉德尔佩 知乎 编辑:程序博客网 时间:2024/06/05 09:27
- package demo.filerw.service;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import android.content.Context;
- import android.os.Environment;
- /**
- * 文件操作类
- * @author janrone
- * @website http://hujl.sinaapp.com
- */
- public class FileService {
- private Context context;
- public FileService(Context context) {
- this.context = context;
- }
- //存储数据到文件
- public void saveName(String name) throws Exception{
- //context.getFilesDir();// 得到存放文件的系统目录 /data/data/<package name>/files
- //context.getCacheDir(); //缓存目录 /data/data/<package name>/cache
- FileOutputStream outputStream=context.openFileOutput("deomfilerw.txt", Context.MODE_APPEND);
- outputStream.write(name.getBytes());
- outputStream.close();
- }
- //存储数据到sdcard
- public void saveNameToSDCard(String name) throws Exception{
- Environment.getExternalStorageDirectory(); //得到sdcard目录
- File file=new File("/sdcard","demosdcard.txt");
- FileOutputStream outputStream=new FileOutputStream(file);
- outputStream.write(name.getBytes());
- outputStream.close();
- }
- // 读取数据
- public String getName() throws Exception{
- FileInputStream inputStream=context.openFileInput("deomfilerw.txt");
- ByteArrayOutputStream outStream=new ByteArrayOutputStream();
- byte[] buffer=new byte[1024];
- int len=0;
- while ((len=inputStream.read(buffer))!=-1){
- outStream.write(buffer, 0, len);
- }
- outStream.close();
- byte[] data=outStream.toByteArray();
- String name=new String(data);
- return name;
- }
- }
事件监听类可以放到外边
- package demo.filerw.clicklistener;
- import android.app.Activity;
- import android.content.Context;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import demo.filerw.R;
- import demo.filerw.service.FileService;
- public class ClickListener implements OnClickListener {
- private FileService fileService;
- private EditText editText;
- private TextView show_text;
- private Context context;
- public ClickListener(Context context) {
- this.context=context;
- fileService = new FileService(context);
- Activity activity = (Activity) context;
- editText = (EditText) activity.findViewById(R.id.edit_name);
- show_text = (TextView) activity.findViewById(R.id.show_text);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.ok_button:
- String name=editText.getText().toString();
- try {
- fileService.saveName(name);
- Toast.makeText(context, R.string.ok_succee, Toast.LENGTH_SHORT).show();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- //Toast.makeText(contex, R.string.ok_error, Toast.LENGTH_SHORT).show();
- }
- break;
- case R.id.read_button:
- try {
- String name1 =fileService.getName();
- show_text.setText(name1);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Toast.makeText(context, R.string.read_error, Toast.LENGTH_SHORT).show();
- }
- break;
- }
- }
- }
Activity
- package demo.filerw;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import demo.filerw.clicklistener.ClickListener;
- public class FileRWActivity extends Activity {
- private Button ok_button;
- private Button read_button;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ok_button = (Button) findViewById(R.id.ok_button);
- read_button = (Button) findViewById(R.id.read_button);
- ok_button.setOnClickListener(new ClickListener(this));
- read_button.setOnClickListener(new ClickListener(this));
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/name_lable"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edit_name"
- />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/ok_button"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/ok_button"
- />
- <Button
- android:id="@+id/read_button"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/read_button"
- />
- </LinearLayout>
- <TextView
- android:id="@+id/show_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
使用 单元 测试 来测试 FileService 类
配置 单元测试 和 配置 SDcard 所需要的权限
- <uses-sdk android:minSdkVersion="10" />
- <!-- 在SDCard中创建与删除文件权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <!-- 往SDCard写入数据权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <instrumentation
- android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="demo.filerw" android:label="ServiceTest"
- />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <uses-library android:name="android.test.runner" />
- <activity
- android:label="@string/app_name"
- android:name=".FileRWActivity" >
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
测试类
- package demo.filerw;
- import demo.filerw.service.FileService;
- import android.os.Environment;
- import android.test.AndroidTestCase;
- import android.util.Log;
- public class FileTest extends AndroidTestCase {
- private static final String TAG="FileTest";
- //存储数据
- public void testSaveName() throws Exception{
- FileService fileService=new FileService(this.getContext());
- fileService.saveName("李明");
- }
- public void testSaveNameToSDCard() throws Exception{
- if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
- FileService fileService = new FileService(this.getContext());
- fileService.saveNameToSDCard("李明和小强");
- } else {
- Log.e(TAG, "sdcard not exsit or only read");
- }
- }
- public void getSaveName() throws Exception{
- FileService fileService=new FileService(this.getContext());
- String name=fileService.getName();
- Log.i(TAG, name);
- }
- }
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- Android文件操作中的openFileOutPut和openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- andriod 内存数据读取 写入操作(openFileInput,openFileOutput)
- 使用openFileOutput和openFileInput读取文件
- Android下openfileoutput和openfileinput的使用
- Android openFileOutput 和 openFileInput
- Android openFileOutput 和 openFileInput
- openFileOutput和openFileInput
- openFileOutput()和openFileInput
- 操作手机内存:openFileOutput、openFileInput
- 文件存储openFileOutput和openFileInput 和String.getBytes()
- 使用openFileInput和openfileOutput方法读写应用程序数据文件夹下的文件
- Hdu 1069 Monkey and Banana -- DP
- 《TCP-IP详解 卷1:协议》学习笔记(一)
- Surround the Trees
- 网络运维与管理2013超值精华本
- 纯技术角度分析目前二代身份证存在的安全缺陷及解决方案
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- hdu 4274 Spy's Work
- VS 2010 快捷键
- 总结的php10个常用的处理字符串的函数事例
- 华为助力Vert巴西分公司部署数据中心
- 反省一下>.<
- java.sql.SQLException: 数据大小超出此类型的最大值
- PCI配置空间简介
- autotools精简二(生成libxml2静态库)