android-读取和保存文件实例

敲多了就会了,哈哈哈!一起加油!

注意路径我是用的雷电模拟器

layout.xml

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/savebtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="保存资源"/>
<Button
    android:id="@+id/savesbtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="保存SD"/>
<Button
    android:id="@+id/readbtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="读取资源"/>
<Button
    android:id="@+id/readsbtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="读取SD"/>

main.java

public class MainActivity extends AppCompatActivity {
Button savebtn,readbtn,savesbtn,readsbtn;
EditText edit;
String fileName = “test.txt”;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = findViewById(R.id.edit);
savebtn = findViewById(R.id.savebtn);
savebtn.setOnClickListener(new mClick());
savesbtn = findViewById(R.id.savesbtn);
savesbtn.setOnClickListener(new mClick());
readbtn = findViewById(R.id.readbtn);
readbtn.setOnClickListener(new mClick());
readsbtn = findViewById(R.id.readsbtn);
readsbtn.setOnClickListener(new mClick());
}
private class mClick implements View.OnClickListener {
@Override
public void onClick(View v) {
if (vsavebtn){
savefile();
}
else if (v
readbtn){
readfile(fileName);
}
else if (vsavesbtn){
savesSDcar();
}
else if (v
readsbtn){
readSDcar(fileName);
}
}
}
void readSDcar(String fileName) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File path = Environment.getExternalStorageDirectory();
File sdfile = new File(path,fileName);
try {
FileInputStream in_file = new FileInputStream(sdfile);
byte[] buffer = new byte[1024];
int bytes = in_file.read(buffer);
str = new String(buffer,0,bytes);
Toast.makeText(MainActivity.this,“文件内容”+str,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
System.out.println(“文件不存在”);
// e.printStackTrace();
} catch (IOException e) {
System.out.println(“IO流错误”);
// e.printStackTrace();
}
}
}
void savesSDcar() {
str = edit.getText().toString();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
{
File path = Environment.getExternalStorageDirectory();
File sdfile = new File(path,fileName);
try {
FileOutputStream f_out = new FileOutputStream(sdfile);
f_out.write(str.getBytes());
Toast.makeText(MainActivity.this,“文件保存SD”,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void readfile(String fileName) {
byte[] buffer = new byte[1024];
try {
FileInputStream in_file = openFileInput(fileName);
int bytes = in_file.read(buffer);
str = new String(buffer,0,bytes);
Toast.makeText(MainActivity.this,“文件内容”+str,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
System.out.println(“文件不存在”);
// e.printStackTrace();
} catch (IOException e) {
System.out.println(“IO流错误”);
// e.printStackTrace();
}
}
void savefile() {
str = edit.getText().toString();
try {
FileOutputStream f_out = openFileOutput(fileName, Context.MODE_PRIVATE);
f_out.write(str.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值