代码改变世界

android 数据存储之文件存储

2013-03-02 17:21  waddell  阅读(171)  评论(0编辑  收藏  举报

FileOperateByActivity.java

package com.example.phonedemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;

public class FileOperateByActivity extends Activity {
    private LayoutParams wrap = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    private LayoutParams match = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    
    private LinearLayout layout = null;
    private Button but = null;
    private EditText text = null;
    
    private String fileName = "waddell";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        this.layout = new LinearLayout(this);
        this.layout.setOrientation(LinearLayout.VERTICAL);
        
        this.but = new Button(this);
        this.but.setText("save");
        this.but.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                System.out.println("** onClick **");
                FileOperateByActivity.this.saveFile();
            }
        });
        this.layout.addView(this.but, wrap);
        
        this.text = new EditText(this);
        this.text.setLines(10);
        this.text.setGravity(Gravity.TOP);
        this.text.setText(getFile());
        this.layout.addView(this.text, wrap);
        super.addContentView(this.layout, match);
        
    }
    
    public String getFile(){
        FileInputStream input = null;
        String str = "";
        try {
            input = super.openFileInput(fileName);
            Scanner scan = new Scanner(input);
            while(scan.hasNext()){
                str += scan.next() + "\n";
            }
            scan.close();
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return str;
    }
    
    public void saveFile(){
        FileOutputStream output = null ;
        try{
            output = super.openFileOutput(fileName, MODE_PRIVATE);
            PrintStream out = new PrintStream(output);
            System.out.println("text: " + this.text.getText());
            out.println(this.text.getText());
            out.close();
            output.close();
        } catch(FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

 

FileOperateByIO.java

package com.example.phonedemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;

public class FileOperateByIO extends Activity {
    private LayoutParams wrap = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    private LayoutParams match = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    private LinearLayout layout = null;
    private Button but = null;
    private EditText text = null;

    private String fileName = "waddell.txt";
    private String dir = "phoneDemo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        this.layout = new LinearLayout(this);
        this.layout.setOrientation(LinearLayout.VERTICAL);

        this.but = new Button(this);
        this.but.setText("save");
        this.but.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                FileOperateByIO.this.saveFile();
            }
        });
        this.layout.addView(this.but, wrap);

        this.text = new EditText(this);
        this.text.setLines(10);
        this.text.setGravity(Gravity.TOP);
        this.text.setText(getFileContent());
        this.layout.addView(this.text, wrap);
        super.addContentView(this.layout, match);
    }

    public String getFileContent() {
        String str = "";
        try {
            Scanner scan = new Scanner(new FileInputStream(getFile()));
            while (scan.hasNext()) {
                str += scan.next() + "\n";
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return str;
    }

    public void saveFile() {

        PrintStream out = null;
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(getFile(), true);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            out = new PrintStream(outputStream, true, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();

    }

    public File getFile() {
        File file = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            file = new File(Environment.getExternalStorageDirectory()
                    .toString()
                    + File.separator
                    + dir
                    + File.separator
                    + fileName);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        return file;
    }
}