1027
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内部存储" /> <EditText android:id="@+id/et_out" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入您想要写入的内容" /> <Button android:id="@+id/btn_out" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入" android:onClick="click_out"/> <EditText android:id="@+id/et_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="读取的内容" /> <Button android:id="@+id/btn_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click_in" android:text="读取" /> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_out; private EditText et_in; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_out = (EditText)findViewById(R.id.et_out); et_in = (EditText)findViewById(R.id.et_in); tv = (TextView) findViewById(R.id.tv); } public void click_out(View view) { String fileName = "data.txt"; String content = et_out.getText().toString(); FileOutputStream fos = null; try { fos = openFileOutput(fileName, MODE_PRIVATE); fos.write(content.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try{ if(fos!=null){ fos.close(); } }catch(IOException e){ e.printStackTrace(); } } Toast.makeText(MainActivity.this, "写入"+content+"成功", Toast.LENGTH_SHORT).show(); } public void click_in(View view) { String content = ""; FileInputStream fis = null; try { fis = openFileInput("data.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); content = new String(buffer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(fis!=null){ fis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } et_in.setText(content); //tv.setText(content); } @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; } }