文件的存与读
别的不多说了 直入话题吧!
布局文件很简单两个文本框、两个按钮分为两组一组用于保存,另一组用于显示 看看吧
public class MainActivity extends Activity { final String FILE_NAME="crazyit.txt"; EditText text1,text2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1=(EditText) findViewById(R.id.text1); text2=(EditText) findViewById(R.id.text2); } public void onclick(View v){ switch (v.getId()) { case R.id.btn1: write(text1.getText().toString()); text1.setText(""); break; case R.id.btn2: text2.setText(read()); break; } } private String read() { // TODO Auto-generated method stub try { FileInputStream fis=openFileInput(FILE_NAME); byte[] buff=new byte[1024]; int len=0; StringBuffer sb=new StringBuffer(""); while((len=fis.read(buff))>0){ sb.append(new String(buff,0,len)); } fis.close(); return sb.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void write(String string) { // TODO Auto-generated method stub try { FileOutputStream fos=openFileOutput(FILE_NAME, MODE_APPEND); PrintStream ps=new PrintStream(fos); ps.println(string); ps.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
我相信这里的大家应该看得懂我就不多作介绍了 ……