Android 读取和保存文件(手机内置存储器)

1:activity_main.xml

<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">

    <TextView 

        android:id="@+id/tv_filename"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="文件名称"/>

    <EditText 

        android:id="@+id/et_filename"

        android:layout_below="@id/tv_filename"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

    

    <TextView 

        android:id="@+id/tv_content"

        android:layout_below="@id/et_filename"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="文件内容"/>

    <EditText 

        android:id="@+id/et_content"

        android:layout_below="@id/tv_content"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

    

    <Button 

        android:id="@+id/btn_save"

        android:layout_below="@id/et_content"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="保存"/>

    

    <Button 

        android:id="@+id/btn_read"

        android:layout_below="@id/btn_save"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="读取"/>

</RelativeLayout>

2:文件操作类:FileService.java

public class FileService {

	private Context context=null;

	

	public FileService(Context context){

		this.context=context;

	}

	

	//save file

	public void saveFile(String filename,String content) throws Exception{

		FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);

		out.write(content.getBytes());

		out.close();

	}

	

	//read file

	public String readFile(String filename)throws Exception{

		FileInputStream is=context.openFileInput(filename);

		byte b[]=new byte[1024];

		int len=0;

		ByteArrayOutputStream baos=new ByteArrayOutputStream();

		//先把数据写入内存

		while((len=is.read(b))!=-1){

			baos.write(b,0,len);

		}

		//从内存中读取数据

		byte data[]=baos.toByteArray();

		

		baos.close();

		is.close();

		

		return new String(data);

	}

}

3:MainActivity.java

public class MainActivity extends Activity {

	private FileService fileService=null;

	private Button btnSave=null,btnRead=null;

	private EditText etFilename=null;

	private EditText etContent=null;

	

	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		

		btnSave=(Button)findViewById(R.id.btn_save);

		btnRead=(Button)findViewById(R.id.btn_read);

		etFilename=(EditText)findViewById(R.id.et_filename);

		etContent=(EditText)findViewById(R.id.et_content);

		

		fileService=new FileService(MainActivity.this);

		

		btnSave.setOnClickListener(new OnClickListener(){

			public void onClick(View view){

				String filename=etFilename.getText().toString();

				String content=etContent.getText().toString();

				try {

					fileService.saveFile(filename, content);

					Toast.makeText(MainActivity.this, "Save file success!", Toast.LENGTH_SHORT).show();

				} catch (Exception e) {

					Toast.makeText(MainActivity.this, "Save file error!", Toast.LENGTH_SHORT).show();

				}

			}

		});

		

		btnRead.setOnClickListener(new OnClickListener(){

			public void onClick(View view){

				String filename=etFilename.getText().toString();

				try {

					String text=fileService.readFile(filename);

					etContent.setText(text);

				} catch (Exception e) {

					Toast.makeText(MainActivity.this, "Read file error!", Toast.LENGTH_SHORT).show();

				}

			}

		});

	}



}

  

4:运行结果

  Android 读取和保存文件(手机内置存储器)

你可能感兴趣的:(android)