标签:
存储分为内存储,外存储(Sd卡存储),其中还涉及IO流的应用。
我们先来看内存储,在布局中EditView中输入的内容,通过点击按钮,分别进行保存,读取,删除的操作,读取时显示在下面的TextView中。
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.jreduch07.InnerIoActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:id="@+id/content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/save"
android:layout_below="@+id/content"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
android:id="@+id/read"
android:layout_below="@+id/content"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:id="@+id/delete"
android:layout_below="@+id/content"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/tv"
android:layout_below="@+id/save"
android:layout_alignParentStart="true" />
</RelativeLayout>
Activity代码如下:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InnerIoActivity extends AppCompatActivity {
private EditText content;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inner_io);
Button save= (Button) findViewById(R.id.save);
Button read= (Button) findViewById(R.id.read);
final Button delete= (Button) findViewById(R.id.delete);
tv= (TextView) findViewById(R.id.tv);
content= (EditText) findViewById(R.id.content);
save.setOnClickListener(new View.OnClickListener() { //保存的点击事件
@Override
public void onClick(View v) {
saveFile();
}
});
read.setOnClickListener(new View.OnClickListener() { //读取的点击事件
@Override
public void onClick(View v) {
tv.setText(readFile());
}
});
delete.setOnClickListener(new View.OnClickListener() { //删除的点击事件
@Override
public void onClick(View v) {
Log.d("==","********");
removeFile();
}
});
}
public void removeFile(){ //删除的方法
String[] files=fileList();
for(String str:files){
if(str.equals("text.txt")){
deleteFile("text.txt");
break;
}
}
}
public String readFile(){ //从内存储中读取文件的方法
BufferedReader reader=null;
FileInputStream fis=null;
StringBuilder sbd=new StringBuilder();
try {
fis=openFileInput("text.txt"); //打开名为text.txt的文件
reader=new BufferedReader(new InputStreamReader(fis));
String row ="";
sbd.append(getFilesDir().getCanonicalPath());
while( (row=reader.readLine()) !=null){
sbd.append(row);
}
} catch (FileNotFoundException e) {
Toast.makeText(getBaseContext(),"文件不存在",Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sbd.toString();
}
//保存文件到内存储的方法
public void saveFile(){
FileOutputStream fos=null;
try {
/*openFileOutput 返回一个 输出字节流
指向的路径为 data/data/包名/files/
参数1:文件名称(如果不存在则自动创建)
参数2:模式MODE_APPEND 文件内容可追加
模式MODE_PRIVATE 文件内容被覆盖
*/
fos= openFileOutput("text.txt",MODE_APPEND);
String str=content.getText().toString();
fos.write(str.getBytes());
Toast.makeText(getBaseContext(),"保存成功",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
外存储即为Sd卡存储,
布局与内存储的相同,只是存储的方法不同而已。
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class SaveToSdCardActivity extends AppCompatActivity {
private Button save,read,delete;
private EditText content;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inner_io);
save= (Button) findViewById(R.id.save);
read= (Button) findViewById(R.id.read);
tv= (TextView) findViewById(R.id.tv);
content= (EditText) findViewById(R.id.content);
delete= (Button) findViewById(R.id.delete);
save.setOnClickListener(new View.OnClickListener() { //保存的点击事件
@Override
public void onClick(View v) {
saveFile();
}
});
read.setOnClickListener(new View.OnClickListener() { //读取的点击事件
@Override
public void onClick(View v) {
tv.setText(readFile());
}
});
delete.setOnClickListener(new View.OnClickListener() { //删除的点击事件
@Override
public void onClick(View v) {
removeFile();
}
});
}
//保存文件到SD卡
public void saveFile(){
FileOutputStream fos=null;
//获取sd状态
String state = Environment.getExternalStorageState();
//判断sd卡是否就绪,只要涉及到Sd卡的方法,就要判断SD卡是否就绪
if(!state.equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this,"请检查sd卡",Toast.LENGTH_SHORT).show();
return;
}
//取得SD卡根目录
File file=Environment.getExternalStorageDirectory();
try {
//File myFile=new File(file.getCanonicalPath()+"/sd.txt",true); //两种不同的方式
/*
输出流的构造参数1:可以是 File 对象,也可以是文件路径
输出流的构造参数2:默认为 false =》覆盖内容; true =》追加内容
*/
fos=new FileOutputStream(file.getCanonicalPath()+"/sd.txt",true);
String str= content.getText().toString();
fos.write(str.getBytes());
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//从SD卡读取文件
public String readFile(){
BufferedReader reader=null;
FileInputStream fis=null;
StringBuilder sbd=new StringBuilder();
String statu=Environment.getExternalStorageState();
if(!statu.equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this,"SD卡未就绪",Toast.LENGTH_SHORT).show();
return "";
}
File root=Environment.getExternalStorageDirectory();
try {
fis=new FileInputStream(root+"/sd.txt");
reader=new BufferedReader(new InputStreamReader(fis));
String row="";
while((row=reader.readLine())!=null){
sbd.append(row);
}
} catch (FileNotFoundException e) {
Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
// e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sbd.toString();
}
//删除SD卡文件
public void removeFile(){
String state= Environment.getExternalStorageState();
//判断sd卡是否就绪
if(!state.equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this,"请检查sd卡",Toast.LENGTH_SHORT).show();
return;
}
File file=Environment.getExternalStorageDirectory();
File myFile = new File(file+"/sd.txt");
if (myFile.exists()){
myFile.delete();
Toast.makeText(this,"文件已删除",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
}
}
}
标签:
原文地址:http://www.cnblogs.com/andzhang/p/5774233.html