完成效果:在文本框中输入数字或文字,点击保存按钮然后再按读取按钮,输入内容显示到按钮下方。
在res下新建raw文件,点击读取raw按钮,输入内容显示到按钮下方。
新建一个assets,点击读取assets按钮,输入内容显示到按钮下方。
点击删除按钮,删除输入内容。
布局1:
代码:
package com.example.administrator.jreduch09;
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 show;
@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);
delete= (Button) findViewById(R.id.delete);
show= (TextView) findViewById(R.id.show);
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) {
show.setText(readFile());
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeFile();
}
});
}
//保存文件到SDcard中
public void saveFile(){
FileOutputStream fos=null;
String state= Environment.getExternalStorageState();
//获取SD卡状态
if(!state.equals(Environment.MEDIA_MOUNTED)){
//判断SD卡是否就绪
Toast.makeText(getBaseContext(),"请检查SD卡",Toast.LENGTH_SHORT).show();
return;
}
File file=Environment.getExternalStorageDirectory();
//取得SD卡根目录
try {
Log.d("===SD卡根目录:",file.getCanonicalPath()+"sd.txt");
fos=new FileOutputStream(file.getCanonicalPath()+"/sd.txt",true);
String str=content.getText().toString();
fos.write(str.getBytes());
Toast.makeText(getBaseContext(),"保存成功",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String readFile(){
BufferedReader reader =null;
FileInputStream fis=null;
//从SD卡读取文件
StringBuilder sdb=new StringBuilder();
String state=Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getBaseContext(),"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){
sdb.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 sdb.toString();
}
public void removeFile(){
String state=Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getBaseContext(),"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();
}
}
}
效果展示:
代码:
package com.example.administrator.jreduch09;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadRawAndAssetsActivity extends AppCompatActivity {
private TextView show;
private Button raw,assets;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_raw_and_assets);
raw= (Button) findViewById(R.id.raw);
assets= (Button) findViewById(R.id.assets);
show= (TextView) findViewById(R.id.show);
raw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.setText(readRaw());
}
});
assets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.setText(readAssets());
}
});
}
public String readRaw(){
StringBuilder sbd=new StringBuilder();
InputStream is=null;
BufferedReader reader=null;
is=getResources().openRawResource(R.raw.settings);
String row="";
try {
reader=new BufferedReader(new InputStreamReader(is));
while ((row=reader.readLine())!=null) {
sbd.append(row);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sbd.toString();
}
public String readAssets(){
StringBuilder sdb=new StringBuilder();
BufferedReader reader=null;
try {
InputStream is=getResources().getAssets().open("city");
reader=new BufferedReader(new InputStreamReader(is));
String row="";
while ((row=reader.readLine())!=null){
sdb.append(row);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
}
}
}
return sdb.toString();
}
}
settings中的内容:
1.大家好我是VAE。 2.那是一条神奇的天路啊啊啊。 3.我愿意为你,我愿意为你。 4.如果你对天空向往。assets中的city的内容:
北京上海广州
效果展示: