Android学习第四天--读取文件
<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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件名"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/filename" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件内容"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:minLines="5" android:gravity="top" android:id="@+id/filecontent" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定" android:id="@+id/submit"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="欢迎欢迎" android:id="@+id/textview" /> </LinearLayout>
页面跳转和读取文件
import android.app.Activity; import android.os.Bundle; public class Activity01 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); } }
import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText editText1,editText2; FileContent fileContent =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1=(EditText)findViewById(R.id.filename); editText2=(EditText)findViewById(R.id.filecontent); Button button=(Button)findViewById(R.id.submit); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { fileContent=new FileContent(MainActivity.this); fileContent.MyWrite(editText1.getText().toString(), editText2.getText().toString()); Intent intent =new Intent(MainActivity.this,Activity01.class); MainActivity.this.startActivity(intent); } }); } }
import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import android.content.Context; public class FileContent { Context context=null; public FileContent(Context context) { this.context = context; } public void MyWrite(String fileName,String fileContent) { try { FileOutputStream fos=context.openFileOutput(fileName,Context.MODE_PRIVATE); OutputStreamWriter osw=new OutputStreamWriter(fos, "utf-8"); BufferedWriter bw=new BufferedWriter(osw); bw.write(fileContent); bw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }