首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android开发-Input/OutputStream操作

2013-01-25 
Android开发--Input/OutputStream操作实现的代码如下所示:public class OutInputStream_Activity_Test ext

Android开发--Input/OutputStream操作

实现的代码如下所示:

public class OutInputStream_Activity_Test extends Activity {private Button button;private Button button2;private EditText editText;private TextView textView;String string;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_out_input_stream__activity__test);button=(Button)findViewById(R.id.button1);button2=(Button)findViewById(R.id.button2);editText=(EditText)findViewById(R.id.edittext);textView=(TextView)findViewById(R.id.textview);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubOutputStream os = null;try {os = openFileOutput("file.txt", Activity.MODE_PRIVATE);} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}string=editText.getText().toString();try {os.write(string.getBytes("utf-8"));editText.setText("");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {InputStream is=openFileInput("file.txt");byte [] buffer=new byte[100];int byteCount=is.read(buffer);String string=new String(buffer, 0, byteCount, "utf-8");textView.setText(string);is.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}});}}


热点排行