-----------------------------------------------
package com.eboy.filereadwrite;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.eboy.filereadwrite.R.id;
import com.eboy.filereadwrite.R.string;
import com.eboy.service.FileService;
public class MainActivity extends Activity {
private EditText edtFileName = null;
private EditText edtFileContent = null;
private Button btnRead = null;
private Button btnWrite = null;
private Button btnSDRead = null;
private Button btnSDWrite = null;
private String FileName;
private String FileContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtFileName = (EditText) findViewById(R.id.FileName);
edtFileContent = (EditText) findViewById(R.id.FileContent);
btnRead = (Button) findViewById(id.btnRead);
btnWrite = (Button) findViewById(id.btnWrite);
btnRead.setOnClickListener(new ReadOnClickListener());
btnWrite.setOnClickListener(new WriteOnClickListener());
btnSDRead = (Button) findViewById(id.btnSDRead);
btnSDWrite = (Button) findViewById(id.btnSDWrite);
btnSDRead.setOnClickListener(new SDReadOnClickListener());
btnSDWrite.setOnClickListener(new SDWriteOnClickListener());
}
private final class WriteOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
service.write(FileName, FileContent);
Toast.makeText(getApplicationContext(), string.writeSuccess, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.writeFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
private final class ReadOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
FileContent = service.read(FileName);
edtFileContent.setText(FileContent);
Toast.makeText(getApplicationContext(), string.readSuccess, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.readFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
private final class SDWriteOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
//判断SD卡是否存在
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
service.writeToSDCard(FileName, FileContent);
} else {
Toast.makeText(getApplicationContext(), string.SDCardNotFound, Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(), string.writeSuccess, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.writeFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
private final class SDReadOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
FileContent = service.readFromSDCard(FileName);
edtFileContent.setText(FileContent);
Toast.makeText(getApplicationContext(), string.readSuccess, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), string.SDCardNotFound, Toast.LENGTH_LONG).show();
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.readFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.eboy.filereadwrite.R.id;
import com.eboy.filereadwrite.R.string;
import com.eboy.service.FileService;
public class MainActivity extends Activity {
private EditText edtFileName = null;
private EditText edtFileContent = null;
private Button btnRead = null;
private Button btnWrite = null;
private Button btnSDRead = null;
private Button btnSDWrite = null;
private String FileName;
private String FileContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtFileName = (EditText) findViewById(R.id.FileName);
edtFileContent = (EditText) findViewById(R.id.FileContent);
btnRead = (Button) findViewById(id.btnRead);
btnWrite = (Button) findViewById(id.btnWrite);
btnRead.setOnClickListener(new ReadOnClickListener());
btnWrite.setOnClickListener(new WriteOnClickListener());
btnSDRead = (Button) findViewById(id.btnSDRead);
btnSDWrite = (Button) findViewById(id.btnSDWrite);
btnSDRead.setOnClickListener(new SDReadOnClickListener());
btnSDWrite.setOnClickListener(new SDWriteOnClickListener());
}
private final class WriteOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
service.write(FileName, FileContent);
Toast.makeText(getApplicationContext(), string.writeSuccess, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.writeFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
private final class ReadOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
FileContent = service.read(FileName);
edtFileContent.setText(FileContent);
Toast.makeText(getApplicationContext(), string.readSuccess, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.readFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
private final class SDWriteOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
//判断SD卡是否存在
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
service.writeToSDCard(FileName, FileContent);
} else {
Toast.makeText(getApplicationContext(), string.SDCardNotFound, Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(), string.writeSuccess, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.writeFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
private final class SDReadOnClickListener implements View.OnClickListener{
public void onClick(View v) {
FileService service = new FileService(getApplicationContext());
FileName = edtFileName.getText().toString();
FileContent = edtFileContent.getText().toString();
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
FileContent = service.readFromSDCard(FileName);
edtFileContent.setText(FileContent);
Toast.makeText(getApplicationContext(), string.readSuccess, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), string.SDCardNotFound, Toast.LENGTH_LONG).show();
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), string.readFail + "," + e.toString(), Toast.LENGTH_LONG).show();
}
}
}