Android 笔记 文件存取 day5

时间:2021-05-24 13:02:28

针对文件的存取

 package com.example.file01;

 import com.example.service.FileService;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.*;
 import android.widget.*;

 public class MainActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         Button button=(Button)this.findViewById(R.id.button1);

         button.setOnClickListener(new ButtonClickListener());

     }

     private class ButtonClickListener implements View.OnClickListener{

         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub
             EditText filename1=(EditText)findViewById(R.id.editText1);
             EditText content1=(EditText)findViewById(R.id.EditText01);
             String filename=filename1.getText().toString();
             String content=content1.getText().toString();
             FileService service=new FileService(getApplicationContext());
             try {
                 service.save(filename,content);

                 Toast.makeText(getApplicationContext(), "success", 1).show();
             } catch (Exception e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();

                 Toast.makeText(getApplicationContext(), "fail", 1).show();
             }

         }

     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }

     @Override
     public 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);
     }
 }
 package com.example.service;

 import java.io.ByteArrayOutputStream;
 import java.io.*;
 import java.io.FileOutputStream;

 import android.content.Context;

 public class FileService {
     private Context context;

     public FileService(Context context) {
         this.context = context;
     }

     /**
      * 保存文件
      * @param filename    文件名
      * @param content    文件内容
      */
     public void save(String filename, String content)throws Exception {
         // TODO Auto-generated method stub
         FileOutputStream outStream=context.openFileOutput(filename, Context.MODE_PRIVATE);

         outStream.write(content.getBytes());
         outStream.close();
     }

     /**
      *
      * @param filename
      * @return
      * @throws Exception
      */
     public String read(String filename)throws Exception {
         FileInputStream inStream=context.openFileInput(filename);
         ByteArrayOutputStream outStream=new ByteArrayOutputStream();
         byte[]    buffer =new byte [1024];
         int len=0;
         while((len=inStream.read(buffer))!=-1){
             outStream.write(buffer,0,len);

         }
         byte[] date=outStream.toByteArray();
         return new String(date);

     }
 }

测试,需要添加一些东西

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.file01"
     android:versionCode="1"
     android:versionName="1.0" >

     <uses-sdk
         android:minSdkVersion="19"
         android:targetSdkVersion="19" />

     <application

         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <uses-library android:name="android.test.runner" />
         <activity
             android:name=".MainActivity"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>

     </application>

         <instrumentation
         android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.example.file01" />
 </manifest>

测试用代码

 package com.example.test;

 import com.example.service.FileService;

 import android.test.AndroidTestCase;
 import android.util.Log;

 public class testfileread extends AndroidTestCase {
     private static final String TAG="wang.txt";
         public void testread() throws Throwable{
             FileService service =new FileService(this.getContext());
             String s=service.read("wang.txt");
             Log.i(TAG, s);
         }
 }