【Android数据存储】- File
个人学习整理,如有不足之处,请不吝指教。转载请注明:@CSU-Max
读写本应用程序数据文件夹中的文件
package com.example.filedemo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
*
* @author CSUMax
* @version 1.0 2014-4-24
*/
public class MainActivity extends Activity {
private Button bt_main_in; // 写入文件按钮
private Button bt_main_out; // 读取文件按钮
private EditText et_main_in;// 输入文本框
private TextView tv_main_out;// 显示文件内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_main_in = (Button) this.findViewById(R.id.bt_main_in);
bt_main_out = (Button) this.findViewById(R.id.bt_main_out);
et_main_in = (EditText) this.findViewById(R.id.et_main_in);
tv_main_out = (TextView) this.findViewById(R.id.tv_main_out);
// 点击按钮将输入框中的内容写到文件中
bt_main_in.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fileName = "demo.txt";
String text = et_main_in.getText().toString();
writeToFile(text, fileName);
et_main_in.setText("");
}
});
// 点击按钮将文件中内容读取出来显示在文本框中
bt_main_out.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fileName = "demo.txt";
tv_main_out.setText(readFromFile(fileName));
}
});
}
/**
* 读取文件
*
* @param fileName
* @return
*/
public String readFromFile(String fileName) {
try {
FileInputStream fis = openFileInput(fileName);
byte[] bytes = new byte[1024];
StringBuffer sb = new StringBuffer();
int i = 0;
i = fis.read(bytes);
if (i > 0) {
sb.append(new String(bytes, 0, i));
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 写入文件
*
* @param text
* 写入的内容
* @param fileName
* 目标文件的文件名
*/
public void writeToFile(String text, String fileName) {
try {
// MODE_APPEND模式写入,即追加模式
FileOutputStream fos = openFileOutput(fileName, MODE_APPEND);
PrintStream ps = new PrintStream(fos);
ps.println(text);
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
读取资源文件中的文件
public class ReadFileUtil {
public static String readFromRaw(Context context, int id) {
String str = "";
try {
// 得到输入流
InputStream is = context.getResources().openRawResource(id);
int length = is.available();
byte[] bytes = new byte[length];
int i = 0;
i = is.read(bytes);
if (i > 0) {
// 设置编码
str = EncodingUtils.getString(bytes, "UTF-8");
}
is.close();
return str;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
public class ReaderTest extends AndroidTestCase {
private static final String TAG = "ReaderTest";
public void testReadFromRaw() {
Log.i(TAG, "---------------");
String str = "Failed";
str = ReadFileUtil.readFromRaw(getContext(), R.raw.demo);
Log.i(TAG, "========>" + str + "<========");
}
}
读写 SD 卡上的文件
public class ReadFileUtil {
public static String readFromSdcard(String fileName) {
String str = "";
try {
FileInputStream fis = new FileInputStream(fileName);
int length = fis.available();
byte[] bytes = new byte[length];
int i = 0;
i = fis.read(bytes);
if (i > 0) {
// 设置编码
str = EncodingUtils.getString(bytes, "UTF-8");
}
fis.close();
return str;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
/**
* --注意向SD卡中的文件写入信息时,要在AndroidManifest.xml文件中加入相应的权限--
*/
public static void writeToSdcard(String text, String fileName){
try {
//构造方法的第二个参数表示追加模式
FileOutputStream fos = new FileOutputStream(fileName, true);
byte[] bytes = text.getBytes();
fos.write(bytes);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ReaderTest extends AndroidTestCase {
private static final String TAG = "ReaderTest";
public void testReadFromSdcard() {
Log.i(TAG, "---------------");
String str = "Failed";
str = ReadFileUtil.readFromSdcard("/mnt/sdcard/demo.txt");
Log.i(TAG, "========>" + str + "<========");
}
public void testWriteToSdcard() {
Log.i(TAG, "---------------");
String text = " Max ";
ReadFileUtil.writeToSdcard(text, "/mnt/sdcard/demo.txt");
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
***************************************************************************
* 转载请注明出处: @CSU-Max http://blog.csdn.net/csu_max *
***************************************************************************
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。