最近在學習安卓開發,有用到讀寫文件的操做,從網上找了好幾個例子,終於成功運行了,分享給你們java
<RelativeLayout 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" > <EditText android:id="@+id/ed1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" /> <EditText android:id="@+id/ed2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/write" android:layout_below="@+id/write" android:layout_marginTop="43dp" android:ems="10" android:inputType="textMultiLine" > <requestFocus /> </EditText> <Button android:id="@+id/write" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/ed1" android:layout_below="@+id/ed1" android:layout_marginTop="30dp" android:text="@string/write" /> <Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/read" android:layout_alignParentBottom="true" android:layout_marginBottom="22dp" android:text="@string/delete" /> <Button android:id="@+id/read" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/ed2" android:layout_below="@+id/ed2" android:layout_marginTop="24dp" android:text="@string/read" /> </RelativeLayout>
package com.example.test; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity { private Button read; private Button write; private EditText ed1; private EditText ed2; private EditText ed3; private Button delete; private Spinner spiEdu=null; private ArrayAdapter<CharSequence> adapteEdu=null; private List<CharSequence> dataEdu=null;//定義一個集合數據 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); read = (Button) findViewById(R.id.read); write = (Button) findViewById(R.id.write); delete = (Button) findViewById(R.id.delete); ed2 = (EditText) findViewById(R.id.ed2); ed1 = (EditText) findViewById(R.id.ed1); write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String str = ed1.getText().toString(); if (!str.equals("")) { write(str); } } }); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { read(); } }); delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { deleteFiles("test.txt"); // String str = ed3.getText().toString(); // if (!str.equals("")) { // deleteFiles("test.txt"); // } else { // ed3.setText(str + ":該文件輸入錯誤或不存在!"); // } } }); } private void write(String content) { try { // 以追加的方式打開文件輸出流 //this.MODE_APPEND追加 //this.MODE_PRIVATE覆蓋 FileOutputStream fileOut = this.openFileOutput("test.txt", this.MODE_APPEND); // 寫入數據 fileOut.write(content.getBytes()); // 關閉文件輸出流 fileOut.close(); } catch (Exception e) { e.printStackTrace(); } } private void read() { try { ed2.setText(""); // 打開文件輸入流 FileInputStream fileInput = this.openFileInput("test.txt"); BufferedReader br = new BufferedReader(new InputStreamReader( fileInput)); String str = null; StringBuilder stb = new StringBuilder(); while ((str = br.readLine()) != null) { stb.append(str); } ed2.setText(stb); /*this.dataEdu=new ArrayList<CharSequence>(); //String tt=ed2.getText().toString(); List list=getList(stb.toString(),"倉房"); //String[] tt2=stb.toString().split(""); for(int i=0;i<list.size();i++){ this.dataEdu.add(list.get(i).toString()); } // this.dataEdu.add("大學"); // this.dataEdu.add("研究生"); // this.dataEdu.add("高中"); this.spiEdu=(Spinner)super.findViewById(R.id.seledu); this.spiEdu.setPrompt("請選擇您的倉位"); this.adapteEdu=new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item,this.dataEdu); this.adapteEdu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); this.spiEdu.setAdapter(this.adapteEdu);*/ } catch (Exception e) { e.printStackTrace(); } } /* public List getList(String str,String type){ List list=new ArrayList(); //String str="倉房:1號倉,2號倉,3號倉,4號倉;貨位:1號貨位,2號貨位,3號貨位,4號貨位"; String[] strs=str.split(";"); for(String s:strs){ if(s.substring(0, s.indexOf(":")).equals(type)){ String tt=s.substring(s.indexOf(":")+1,s.length()); String[] tts=tt.split(","); for(String t:tts){ // System.out.println(t); list.add(t); } } } return list; }*/ // 刪除指定的文件 private void deleteFiles(String fileName) { try { // 獲取data文件中的全部文件列表 List<String> name = Arrays.asList(this.fileList()); // this.deleteFiles(fileName); if (name.contains(fileName)) { this.deleteFile(fileName); showMessage(fileName + ":該文件成功刪除!"); } else showMessage(fileName + ":該文件輸入錯誤或不存在!"); } catch (Exception e) { e.printStackTrace(); } } @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; } // 顯示信息 public void showMessage(String msg) { Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } }
其中有個方法是我用來讀取文件內容,生成下拉列表的,部分代碼沒有刪除,你們在運行的時候須要注意!android