读数据:输入流
package com.example.day013_internalstorag;
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.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
tv_show = (TextView) findViewById(R.id.tv_show);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.button1://写入数据
System.out.println("AAAAAAA");
OutputStream outputStream=null;
try {
outputStream = openFileOutput("info.txt", MODE_PRIVATE);
outputStream.write(et.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();
}
}
}
break;
case R.id.button2://读出数据
StringBuffer stringBuffer = new StringBuffer();
BufferedReader br = null;
try {
InputStream inputStream = openFileInput("info.txt");
//将字节流转换成字符缓冲流
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line=br.readLine())!=null) {
stringBuffer.append(line);
}
tv_show.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 (br!=null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
break;
case R.id.button3://写入数据2
//getFilesDir()是File文件的路径
File file = new File(getFilesDir(),"test.txt");
FileOutputStream outputStream2 = null;
try {
outputStream2 = new FileOutputStream(file);
outputStream2.write(et.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{
try {
outputStream2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case R.id.button4://读出数据2
//getFilesDir()是File文件的路径
File file1 = new File(getFilesDir(),"test.txt");
ByteArrayOutputStream outputStream3 = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file1);
byte[] buffer = new byte[1024];
int temp = 0;
while((temp=fileInputStream.read(buffer))!=-1){
outputStream3 = new ByteArrayOutputStream();
outputStream3.write(buffer, 0, temp);
outputStream3.flush();
}
tv_show.setText(outputStream3.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (outputStream3!=null) {
try {
outputStream3.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (fileInputStream!=null) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
break;
case R.id.button5://写入数据3
File file2;
FileOutputStream outputStream4 = null;
try {
/**
* 缓存文件
* 参数1:文件的名称
* 参数2:文件的后缀名
* 参数3:文件的保存路径
*/
file2 = File.createTempFile("temp", null,getCacheDir());
outputStream4 = new FileOutputStream(file2);
outputStream4.write(et.getText().toString().trim().getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (outputStream4!=null) {
try {
outputStream4.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
break;
case R.id.button6://读出数据3
File file3 = new File(getCacheDir(),"temp17583.tmp");
StringBuffer stringBuffer2 = new StringBuffer();
FileInputStream fileInputStream2 = null;
BufferedReader br1 = null;
try {
fileInputStream2 = new FileInputStream(file3);
br1 = new BufferedReader(new InputStreamReader(fileInputStream2));
String line = null;
while(br1.readLine()!=null){
stringBuffer2.append(line);
}
tv_show.setText(stringBuffer2.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br1!=null){
try {
br1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fileInputStream2!=null) {
try {
fileInputStream2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
break;
case R.id.button7:
//删除数据
Boolean b = deleteFile("info.txt");
if (b) {
Toast.makeText(MainActivity.this, "删除成功", 0).show();
}else{
Toast.makeText(MainActivity.this, "删除失败", 0).show();
}
break;
default:
break;
}
}
}