package com.king.android.db;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.king.android.R;
/**
* 描述:文件存储
* 作者:Andy.Liu
* 时间: 2012-7-9 上午08:06:23
**/
public class FileStoreActivity extends Activity implements OnClickListener{
/**保存字符串的key*/
public static final String STRING_KEY ="string_key";
/**保存字符串的的名字*/
public static final String STRING_NAME ="string_name";
public static final String FILE_NAME = "hello.txt"; //保存的文件名
Context mContext = null;
EditText edSave,edRead;
Button btnSave,btnRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = FileStoreActivity.this;
initView();
}
private void initView(){
setContentView(R.layout.store_layout);
edSave = (EditText) findViewById(R.id.ed_edit);
edRead = (EditText) findViewById(R.id.ed_show);
btnSave = (Button) findViewById(R.id.btn_save);
btnSave.setOnClickListener(this);
btnRead = (Button) findViewById(R.id.btn_read);
btnRead.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_save:
FileStoreTools.saveFile(edSave.getText().toString(), FileStoreTools.getSDPath()+File.separator+FILE_NAME);
break;
case R.id.btn_read:
edRead.setText(FileStoreTools.readFile(FileStoreTools.getSDPath()+File.separator+FILE_NAME));
break;
}
}
final static class FileStoreTools{
/**
*
*TODO:保存文件 根目录
*Author:Andy.Liu
*Create Time:2012-7-10 上午08:54:14
*TAG:@return
*Return:String
*/
public static String getSDPath(){
boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(hasSDCard){
return Environment.getExternalStorageDirectory().toString();
}else
return Environment.getDownloadCacheDirectory().toString();
}
/**
*
*TODO:保存文件
*Author:Andy.Liu
*Create Time:2012-7-10 上午08:42:40
*TAG:@param str 文件的内容
*TAG:@param filePath 保存路径
*Return:void
*/
public static void saveFile(String str,String filePath){
FileOutputStream fos = null;
try {
File file = new File(filePath);
if(!file.exists())
file.mkdirs();
fos = new FileOutputStream(file);
fos.write(str.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if(null!=fos)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
*TODO:读取文件
*Author:Andy.Liu
*Create Time:2012-7-10 上午08:48:40
*TAG:@param filePath
*TAG:@return
*Return:String
*/
public static String readFile(String filePath){
FileInputStream fis = null;
byte[] mByte = new byte[512];
try {
fis = new FileInputStream(new File(filePath));
fis.read(mByte);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null!=fis)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(mByte).toString();
}
}
}