鸟语天空
保存文件到内部存储器
post by:追风剑情 2015-8-12 21:39

视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please enter some text" />
    
    <EditText 
        android:id="@+id/txtText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    <Button 
        android:id="@+id/btnSave"
        android:text="Save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickSave" />

    <Button 
        android:id="@+id/btnLoad"
        android:text="Load"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickLoad" />
</LinearLayout>

代码

package com.example.androidtest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class FilesActivity extends Activity {

	EditText textBox;
	static final int READ_BLOCK_SIZE = 100;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_files);
		
		textBox = (EditText)findViewById(R.id.txtText1);
	}
	
	public void onClickSave(View view)
	{
		String str = textBox.getText().toString();
		try {
			//MODE_WORLD_READABLE : 表示此文件可被其他所有应用程序读取
			//MODE_PRIVATE : 文件只能被创建它的应用程序访问
			//MODE_APPEND : 附加到现有文件
			//MODE_WORLD_WRITEABLE : 文件对于其他所有应用程序来说都是可写的
			FileOutputStream fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);
			
			osw.write(str);
			osw.flush();//保证所有字节都写入文件
			osw.close();//关闭文件
			
			Toast.makeText(getBaseContext(), "File saved successfully", Toast.LENGTH_SHORT).show();
			textBox.setText("");
			
		}catch(IOException e){
			Log.e("AndroidTest", e.getLocalizedMessage());
		}
	}
	
	public void onClickLoad(View view)
	{
		try {
			FileInputStream fIn = openFileInput("textfile.txt");
			InputStreamReader isr = new InputStreamReader(fIn);
			char[] inputBuffer = new char[READ_BLOCK_SIZE];
			String s = "";
			
			int charRead;
			while((charRead = isr.read(inputBuffer))>0){
				String readString = String.copyValueOf(inputBuffer, 0, charRead);
				s += readString;
				
				inputBuffer = new char[READ_BLOCK_SIZE]; 
			}
			
			textBox.setText(s);
			
			Toast.makeText(getBaseContext(), "File loaded successfully!", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Log.e("AndroidTest", e.getLocalizedMessage());
		}
	}
}

运行效果

1111.png22222.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容