package com.zdsoft.file1206;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_name, et_content;
private Button bt_write, bt_read, bt_write_sd, bt_read_sd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
listener();
}
private void initView() {
et_name = (EditText) findViewById(R.id.et_name);
et_content = (EditText) findViewById(R.id.et_content);
bt_write = (Button) findViewById(R.id.bt_write);
bt_read = (Button) findViewById(R.id.bt_read);
bt_write_sd = (Button) findViewById(R.id.bt_write_sd);
bt_read_sd = (Button) findViewById(R.id.bt_read_sd);
}
private void listener() {
bt_write.setOnClickListener(this);
bt_read.setOnClickListener(this);
bt_write_sd.setOnClickListener(this);
bt_read_sd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_write:
write();
break;
case R.id.bt_read:
read();
break;
case R.id.bt_write_sd:
writeSD();
break;
case R.id.bt_read_sd:
readSD();
break;
default:
break;
}
}
private void write() {
try {
FileOutputStream fos = openFileOutput(et_name.getText().toString(), Context.MODE_APPEND);
fos.write(et_content.getText().toString().getBytes());
fos.close();
Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void read() {
try {
FileInputStream fis = openFileInput(et_name.getText().toString());
byte[] b = new byte[1024];
int num = 0;
StringBuffer sb = new StringBuffer();
while ((num = fis.read(b)) != -1) {
sb.append(new String(b, 0, num));
}
Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeSD() {
try {
// if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// Toast.makeText(MainActivity.this, "内存卡不存在", Toast.LENGTH_SHORT).show();
// return;
// }
File file = new File(Environment.getExternalStorageDirectory() + "/" + et_name.getText().toString());
FileOutputStream fos = new FileOutputStream(file);
fos.write(et_content.getText().toString().getBytes());
fos.close();
Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void readSD() {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/" + et_name.getText().toString());
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[1024];
int num = 0;
StringBuffer sb = new StringBuffer();
while ((num = fis.read(b)) != -1) {
sb.append(new String(b, 0, num));
}
Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
加SD卡权限