Android 文件保存与读取
in 技术 with 0 comment

Android 文件保存与读取

in 技术 with 0 comment

控件层代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/filename" 
        />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@null"
        android:id="@+id/filename" 
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/filecontent" 
        />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="3"
        android:hint="@null"
        android:id="@+id/filecontent" 
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" 
        android:id="@+id/button" 
        />
</LinearLayout>

逻辑层代码

package com.example.files;

import com.example.service.FileService;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button button= (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());
    }
    
    private final class ButtonClickListener implements View.OnClickListener{

        @SuppressLint("ShowToast") @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            EditText filenameText = (EditText) findViewById(R.id.filename);
            EditText contentText = (EditText) findViewById(R.id.filecontent);
            String filename = filenameText.getText().toString();
            String content = contentText.getText().toString();
            
            FileService service = new FileService(getApplicationContext());
            
            try {
                service.save(filename,content);
                Toast.makeText(getApplicationContext(), R.string.success, 1).show();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), R.string.fail, 1).show();
                e.printStackTrace();
                
            }
            
            
            
        }
        
        
        
    }


 
}

处理层代码

package com.example.service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.Context;

public class FileService {
    
    private Context context;

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

    /**
     * 保存文件
     * @param filename 文件名称
     * @param content 文件内容
     */
    public void save(String filename, String content) throws Exception {
 
        // IO j2ee
        //私有操作模式,创建出来的文件只能被本应用方法,其他应用无法放该文件,另外采用私有模式创建的文件,写入的内容会覆盖原文件内容
     
        FileOutputStream outStream = context.openFileOutput(filename,Context.MODE_PRIVATE);
        outStream.write(content.getBytes());
        outStream.close();
    }
    
    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[] data = outStream.toByteArray();
        return new String(data);
  
    }
     
    
}

Responses