[连载 4/15] Android 从入门到精通Example 之 Save a File

将用户输入的文本保存到android文件系统中。文件被保存在android文件系统的/data/data/your.packet.name.appname/files/folder, 查看这个文件可以通过在eclipse -> Windows -> Open Perspective > Other -> DDMS -> File Explore -> data -> data ...-> samplefile.txt 选中后 点击右上脚 “Pull a file from device”

 

1. SaveFile.java

public class ReadFile extends Activity implements View.OnClickListener { private static final String TAG = "ReadFileActivity"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Button readButton = (Button) findViewById(R.id.button1); readButton.setOnClickListener(this); Button clearButton = (Button) findViewById(R.id.clearbutton); clearButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView textArea = (TextView) findViewById(R.id.filecontent); textArea.setText(""); } }); } public void onClick(View arg0) { doReadFromFile(this); } private void doReadFromFile(Context context) { InputStream istream = null; BufferedReader br = null; String fileContent = "", line=""; try { istream = (InputStream) context.getResources().openRawResource(R.raw.mytext); br = new BufferedReader(new InputStreamReader(istream)); while ( (line = br.readLine()) != null) { fileContent += line + "/n"; } br.close(); istream.close(); } catch (Exception e){ Log.e(TAG, "Found exception when reading file: " + e); } Log.i(TAG, "file content = " + fileContent ); TextView showContent = (TextView) findViewById(R.id.filecontent); showContent.setText(fileContent); } }

 

你可能感兴趣的:(eclipse,android,exception,windows,File,button)