Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86729
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 248
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-02 18:03
文章分类
文章存档

2014年(29)

我的朋友

分类: 网络与安全

2014-04-18 09:06:10

package com.example.zzwinternalstorage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity
{
   private TextView tv;

   public void btn_save(View v)
   {
       FileOutputStream fos = null;
       try
       {
           fos = openFileOutput("zzw.json", MODE_PRIVATE);
           String name = "我是谁";
           fos.write(name.getBytes());
       }
       catch (FileNotFoundException e)
       {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       catch (IOException e)
       {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       finally
       {
           if (fos != null)
           {
               try
               {
                   fos.close();
               }
               catch (IOException e)
               {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           }
       }

   }

   public void btn_read(View v)
   {
       FileInputStream fis = null;
       ByteArrayBuffer arrayBuffer = new ByteArrayBuffer(1024);
       try
       {
           fis = openFileInput("zzw.json");
           int len = 0;
           byte[] buffer = new byte[1024];
           while (-1 != (len = fis.read(buffer)))
           {
               arrayBuffer.append(buffer, 0, len);
           }
       }
       catch (FileNotFoundException e)
       {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       catch (IOException e)
       {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }finally{
           if(fis!=null){
               try
               {
                   fis.close();
               }
               catch (IOException e)
               {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
           }
       }
       String str=new String(arrayBuffer.toByteArray(), 0, arrayBuffer.length());
       tv.setText(str);

   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       tv = (TextView) findViewById(R.id.textView1);
   }

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

}
阅读(1446) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

CU博客助理2014-05-22 13:06:01

嘉宾点评:
只一个脚本,没有其它说明,联系不到上下文,有些无从说起。(感谢您参与“原创博文评选”获奖结果即将公布)