【数据存储】利用Activity类操作数据文件

 

保存数据到txt文本

复制代码
private static final String FILENAME = "d.txt";
private TextView tv_msg = null;



/** 保存数据到txt文本 */
        FileOutputStream output = null;  // 接收文件输出对象
        try {
            // 设置输出的文本名称,及文件创建方式
            output = super.openFileOutput(FILENAME, Activity.MODE_PRIVATE);
            // 打印流包装
            PrintStream out = new PrintStream(output);
            out.print("姓名:张三,");
            out.print("年龄:18,");
            out.println("地址:中国香港.");
            out.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
复制代码

 

读取数据

复制代码
FileInputStream input = null;
        try {
            // 取得输入流
            input = super.openFileInput(FILENAME);
            Scanner scan = new Scanner(input);
            while (scan.hasNext()) {
                this.tv_msg.append(scan.next() + "\n");
            }
            scan.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on   大米稀饭  阅读(192)  评论(0编辑  收藏  举报
努力加载评论中...

点击右上角即可分享
微信分享提示