android手机内存中的文件操作

android为手机存储范围内的文件操作提供了openFileOutput和openFileInputAPI,对文件操作提供了便利,一下是一个小demo,供参考学习使用

mainactivity

package com.opfiletext.example;

import java.io.IOException;

import com.opfiletext.service.FileService;

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

public class MainActivity extends Activity {
	
	private EditText edit_filename, edit_filecontent;
	private Button submit, read;
	private TextView tv;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edit_filename = (EditText)findViewById(R.id.filename);
        edit_filecontent = (EditText)findViewById(R.id.finecontent);
        tv = (TextView)findViewById(R.id.tv);
        
        submit = (Button)findViewById(R.id.submit);
        submit.setOnClickListener(new SubminOnClickListener());
    
        read = (Button)findViewById(R.id.read);
        read.setOnClickListener(new ReadOnClickListener());
    }
    
    
    private final class SubminOnClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			String filename = edit_filename.getText().toString();
			String filecontent = edit_filecontent.getText().toString();
			
			FileService fm = new FileService(MainActivity.this);
			try {
				fm.save(filename, filecontent);
				Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
			} catch (IOException e) {
				Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
		}
    }
    
    private final class ReadOnClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			String filename = edit_filename.getText().toString();
			FileService fm = new FileService(MainActivity.this);
			try {
				String content = fm.read(filename);
				tv.setText(content);
			} catch (IOException e) {
				tv.setText("解析异常");
				e.printStackTrace();
			}
		}
    }
}

serviceclass

package com.opfiletext.service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileService {
	
	private Context context;
	
	public FileService(Context context) {
		this.context = context;
	}

	public void save(String filename, String filecontent) throws IOException {
		FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
		fos.write(filecontent.getBytes());
		fos.close();
	}
	
	public String read(String filename) throws IOException{
		FileInputStream fis = context.openFileInput(filename);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int len = 0;
		while((len = fis.read(b)) > 0){
			baos.write(b, 0, len);
		}
		byte[] data = baos.toByteArray();
		return new String(data);
	}
}

相关推荐