把文件保存在手机的内部存储空间中
1 首先必须在清单文件中添加权限
1、我们来看下业务层的代码,业务层的异常不要进行处理交给控制层activity进行处理,activity通过业务层返回的异常就知道业务是否操作成功,就可以在界面上做出相应的显示了
package contract.test.savafileapplication;import android.content.Context;
import android.os.Environment;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileService {private Context context;public FileService(Context context) {this.context = context;}public void saveTosdCard(String fileName, String fileContext) throws Exception {File file = new File(Environment.getExternalStorageDirectory(),fileName);FileOutputStream fileOutputStream = new FileOutputStream(file);fileOutputStream.write(fileContext.getBytes());fileOutputStream.close();}public String readFromSdCard(String filename) throws IOException{File file = new File(Environment.getExternalStorageDirectory(),filename);FileInputStream fileInputStream = new FileInputStream(file);byte[] bytes = new byte[1024];ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();/*byte是基本数据类型 如int类型Byte是byte的包装类*/int len = 0;while ((len = fileInputStream.read(bytes))!=-1){byteArrayOutputStream.write(bytes,0,len);}byteArrayOutputStream.close();fileInputStream.close();String str = new String(byteArrayOutputStream.toString());return str;}
}
activity的代码:
package contract.test.savafileapplication;import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import java.io.IOException;public class MyActivity extends Activity {private Button save , show;private EditText filename, filecontext;private TextView showContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my);save = (Button)this.findViewById(R.id.saveButton);show = (Button)this.findViewById(R.id.showButton);filename = (EditText)this.findViewById(R.id.fileName_ET);filecontext = (EditText)this.findViewById(R.id.saveContext_ET);showContext = (TextView)this.findViewById(R.id.showConTextView);save.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String name = filename.getText().toString();String context = filecontext.getText().toString();FileService fileService = new FileService(getApplicationContext());try {if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){fileService.saveTosdCard(name,context);Toast.makeText(getApplicationContext(),"文件保存成功!",Toast.LENGTH_LONG).show();}else{Toast.makeText(getApplicationContext(),"sdCard不存在或者写保护!",Toast.LENGTH_LONG).show();}} catch (Exception e) {Toast.makeText(getApplicationContext(),"文件保存失败!",Toast.LENGTH_LONG).show();e.printStackTrace();}}});show.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {FileService fileService = new FileService(getApplicationContext());String name = filename.getText().toString();try {if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){String str = fileService.readFromSdCard(name);Toast.makeText(getApplicationContext(),"文件读取成功!",Toast.LENGTH_LONG).show();showContext.setText(str);}else{Toast.makeText(getApplicationContext(),"sdCard不存在或者写保护!",Toast.LENGTH_LONG).show();}} catch (IOException e) {e.printStackTrace();Toast.makeText(getApplicationContext(),"文件读取失败!",Toast.LENGTH_LONG).show();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}
}
xml的代码:
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:orientation&#61;"vertical"android:layout_width&#61;"fill_parent"android:layout_height&#61;"fill_parent"><TextViewandroid:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"android:text&#61;"请输入要保存的文件名:"/><EditTextandroid:id&#61;"&#64;&#43;id/fileName_ET"android:layout_width&#61;"fill_parent"android:layout_height&#61;"50dp"android:hint&#61;"请输入要保存的文件名&#xff01;"/><TextViewandroid:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"android:text&#61;"请您输入要保存的内容&#xff1a;"/><EditTextandroid:id&#61;"&#64;&#43;id/saveContext_ET"android:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"android:hint&#61;"请您在此处输入文件内容&#xff01;"/><Buttonandroid:id&#61;"&#64;&#43;id/saveButton"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"save"/><Buttonandroid:id&#61;"&#64;&#43;id/showButton"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"show"/><TextViewandroid:id&#61;"&#64;&#43;id/showConTextView"android:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"/>
LinearLayout>