package abc.test; import java.io.BufferedReader; //import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; //import android.os.Environment; import android.widget.ArrayAdapter; import android.widget.ListView; public class FileIOActivity extends Activity { private ListView listView = null; private List<String> listStr = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); WriteFile(); ReadFile(); listView = new ListView(this); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listStr)); setContentView(listView); } private void WriteFile(){ try { FileOutputStream fos = openFileOutput("myText.txt", MODE_PRIVATE); OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write("zhangsan/n"); osw.write("lisi/n"); osw.write("wangwu/n"); osw.write("zhengxin/n"); osw.write("xiaoquan/n"); osw.write("lihuaye/n"); osw.close(); } catch(Exception e){ e.printStackTrace(); } } private void ReadFile(){ try { // File sdcardPath = Environment.getExternalStorageDirectory(); FileInputStream fis = openFileInput("myText.txt"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line; while((line = br.readLine()) != null){ listStr.add(line); } } catch (Exception e) { e.printStackTrace(); } } }