您的位置:首页 > 其它

数据存储之内部存储

2015-07-28 23:17 337 查看
package com.example.internalstorage;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

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

public class MainActivity extends Activity {

private EditText editText;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText_data);
textView = (TextView) findViewById(R.id.textView_result);
}

/**
* 将editext 内容写到内部存储
*
* @param v
*/
public void clickWrite(View v) {
// 创建内部存储输入流对象
OutputStream outputStream = null;
try {
outputStream = openFileOutput("info.txt", Context.MODE_PRIVATE);
outputStream.write(editText.getText().toString().trim().getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭流对象
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/**
* 读取内部存储数据
*
* @param view
*/
public void clickRead(View view) {
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer();
try {
InputStream inputStream = openFileInput("info.txt");
// 将读取字节流转换成字符缓冲流
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
textView.setText(stringBuffer.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭字节缓冲流
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/**
* 内部存储的写入
*
* @param view
*/
public void clickwrite2(View view) {
// 根据内部存储的路径创建file对象 getFilesDir() 获取内部存储的路径
File file = new File(getFilesDir(), "test.txt");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(editText.getText().toString().trim().getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 读取内部存储的内容
* @param view
*/
public void clickread2(View view){
File file=new File(getFilesDir(), "test.txt");
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(file);
int temp=0;
byte[] buff=new byte[1024];
while((temp=inputStream.read(buff))!=-1){
outputStream.write(buff, 0,temp);
outputStream.flush();
}
textView.setText(outputStream.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

/**
* 写入内部存储的缓存文件
* @param view
*/
public void writeCache(View view){
FileOutputStream outputStream=null;
try {
//创建内部存储的缓存文件对象   1参数表示文件名称 2参数表示文件名称的后缀 如果为null .tmp 3参数表示内部缓存文件的路径
File file=File.createTempFile("test",null, getCacheDir());
outputStream=new FileOutputStream(file);
outputStream.write(editText.getText().toString().getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 读取内部存储的缓存文件
* @param view
*/
public void readCache(View view){
StringBuilder sb=new StringBuilder();
try {
File file3 = new File(getCacheDir(), "test11263086940.tmp");
 //File file=File.createTempFile("test178037233.tmp", null,getCacheDir());
FileInputStream inputStream=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while((line=br.readLine())!=null){
sb.append(line);
}
textView.setText(sb.toString());
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除内部存储中的文件
* @param view
*/
public void delete(View view){
boolean bl=deleteFile("info.txt");
System.out.println("文件删除是否成功?"+bl);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: