有些时候需要把从别的地方获取的数据以文件的形式存储在手机上,在需要的时候再读取出来,这便需要熟悉文件的I/O操作。 今天就简单的把文件的I/O操作的实验记录简单的描述一下。 方法主要参考:《深入浅出Google Andorid》一书。 需要了解的是每个应用程序包都会有一个私有的存储数据的目录(类似文件夹),只有属于该包的应用程序才能写入该目录空间,每个包应用程序的私有数据目录位于Android绝对路径/data/data/<包名>/目录中。除了私有数据目录应用程序还拥有/sdcard目录(即SD Card的写入权限)。文件系统中其他系统目录,第三方应用程序是不可写入的。稍后我们在终端下借用“adb shell”进入Android系统查看一下我们的文件写入是否真正成功。 照旧三部曲:xml文件配置界面,java程序实现业务逻辑,然后再实践检验一下。 Step1、界面布局:依然是layout/main.xml <? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:background ="#ffffff"
>

< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/hello"
android:textColor ="#0000ff"
/>

< EditText
android:hint ="Input here:"
android:id ="@+id/input"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
>
</ EditText >
< RelativeLayout android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
>
< Button android:text ="Store"
android:id ="@+id/btn_store"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" >
</ Button >
< Button
android:text ="Extract"
android:id ="@+id/btn_extract"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_toRightOf ="@id/btn_store" >
</ Button >
</ RelativeLayout >

< TextView
android:text ="The store info will be displayed here."
android:id ="@+id/display"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:textColor ="#0000ff"
>
</ TextView >
</ LinearLayout >
Step2、Java代码 package com.penguin7.filetest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FileTest extends Activity {
private final String FILE_PATH = "/data/data/com.penguin7.filetest/";
private final String FILE_NAME = "filetest.txt";

// Definition file
File file;
FileOutputStream out;
FileInputStream in;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// execute store action
Button btnStore = (Button) findViewById(R.id.btn_store);
btnStore.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View arg0) {
try {
// Create a file
file = new File(FILE_PATH, FILE_NAME);
file.createNewFile();

// Open output stream for the file
out = new FileOutputStream(file);

// Get the content and store into the file
EditText et = (EditText) findViewById(R.id.input);
String info = et.getText().toString();
// Transform the string to byte array then store into file
out.write(info.getBytes());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

// Execute read action
Button btnExtract = (Button) findViewById(R.id.btn_extract);
btnExtract.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View arg0) {
try {
// Create a file object to prepare read file's content
file = new File(FILE_PATH, FILE_NAME);
if (!file.exists()) {
return;
}
// Open input stream for file
in = new FileInputStream(file);
// Get length of file and apply to a byte array to store file's information
int length = ( int) file.length();
byte[] temp = new byte[length];
in.read(temp, 0, length);
TextView tv = (TextView) findViewById(R.id.display);
// Before display the content we should transform to UTF-8 coding way
tv.setText(EncodingUtils.getString(temp, "UTF-8"));
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
}
} Step3、查看效果 1、启动程序 2、输入文字后,点击存储按钮,再点击提取按钮,效果图如下 3、最终使用adb shell查看文件是否真实存在并写入数据

更多相关文章

  1. 浅谈Android系统编译apk后so文件在dlopen时出现linker权限问题
  2. android 小型音乐播放器(实现播放、下一首、上一首、自动播放、随
  3. Android(安卓)Q分区存储权限变更及适配
  4. Android中利用ZipEntry漏洞实现免root写恶意文件到应用的沙盒中
  5. iOS /Android多渠道打包方案、重签名方案
  6. Android笔记之开发中处理异常并写入本地log文件
  7. Android(安卓)Studio初使用之百度地图初使用(一)--配置
  8. 【Android7.1.2源码解析系列】Android编译系统翻译------Android
  9. Android(安卓)Studio中布局文件(如activity_main.xml)设计视图&代

随机推荐

  1. android RelativeLayout 适用场景和属性
  2. “APP_NAME" IS NOT TRANSLATED IN ZH, Z
  3. Android之设置页面(PreferenceActivity使
  4. TextUtils类-Android字符串处理类
  5. Android: android自适应屏幕方向和大小
  6. xml ----editview
  7. Android(安卓)assets 目录介绍和应用
  8. Android应用程序内存优化
  9. Android(安卓)Framework中的PolicyManage
  10. Android编译过程详解(三)