您好,登录后才能下订单哦!
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;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。