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