博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 数据存储之文件存储
阅读量:5052 次
发布时间:2019-06-12

本文共 6543 字,大约阅读时间需要 21 分钟。

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;    }}

 

 

 

转载于:https://www.cnblogs.com/waddell/archive/2013/03/02/3394713.html

你可能感兴趣的文章
使用brew安装软件
查看>>
[BZOJ1083] [SCOI2005] 繁忙的都市 (kruskal)
查看>>
Centos6.4安装JDK
查看>>
201521123069 《Java程序设计》 第4周学习总结
查看>>
线性表的顺序存储——线性表的本质和操作
查看>>
【linux】重置fedora root密码
查看>>
pig自定义UDF
查看>>
输入名字显示其生日,没有则让输入生日,做记录
查看>>
Kubernetes 运维学习笔记
查看>>
并查集 经典 畅通工程
查看>>
Spark MLlib 之 Naive Bayes
查看>>
php修改SESSION的有效生存时间
查看>>
spring security 11种过滤器介绍
查看>>
Hibernate一对多、多对一关联
查看>>
一、记录Git使用中遇到的问题及解决方法
查看>>
学习网址
查看>>
前端表格插件datatables
查看>>
内部类
查看>>
树链剖分入门
查看>>
图解算法时间复杂度
查看>>