关于openFileOutput及文件存储,删除,读取简单用法
2012-10-12 00:00
239 查看
package operate.File; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import android.content.Context; public class FileService { Context context; public FileService(Context context){ this.context=context; } public void FileSave(String filename,String content) throws IOException{ FileOutputStream fos=context.openFileOutput(filename, Context.MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } public String FileRead(String filename) throws IOException{ FileInputStream fin=context.openFileInput(filename); byte[] b=new byte[fin.available()]; ByteArrayOutputStream buffer=new ByteArrayOutputStream(); while((fin.read(b))!=-1){ buffer.write(b); } byte[] data; data=buffer.toByteArray(); buffer.close(); fin.close(); return new String(data); } }
Activity代码
package operate.File; import java.io.File; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class OpfActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ Button button,rbutton,dbutton; EditText filename,content; FileService service; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Init(); } public void Init(){ button=(Button)findViewById(R.id.button); rbutton=(Button)findViewById(R.id.rbutton); dbutton=(Button)findViewById(R.id.dbutton); filename=(EditText)findViewById(R.id.filename); content=(EditText)findViewById(R.id.content); service=new FileService(this); button.setOnClickListener(this); rbutton.setOnClickListener(this); dbutton.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v==button){ try { service.FileSave(filename.getText().toString(), content.getText().toString()); Toast.makeText(OpfActivity.this, R.string.success, 1).show(); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText(OpfActivity.this, R.string.failure, 1).show(); Log.i("错误提示", e.toString()); } } if(v==rbutton){ try { Toast.makeText(OpfActivity.this, service.FileRead(filename.getText().toString()), 1).show(); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText(OpfActivity.this, R.string.notfind, 1).show(); } } if(v==dbutton){ File f=new File(getFilesDir(), filename.getText().toString()); if(f.exists()){ Toast.makeText(OpfActivity.this, "删除成功", 1).show(); f.delete(); } } } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/filename" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/content" android:minLines="3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rbutton" android:id="@+id/rbutton"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/delete" android:id="@+id/dbutton"/> </LinearLayout>
string XML
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, OpfActivity!</string> <string name="app_name">Opf</string> <string name="filename">输入文件名称</string> <string name="content">输入文件内容</string> <string name="button">保存</string> <string name="rbutton">读取</string> <string name="success">文件保存成功</string> <string name="failure">文件保存失败</string> <string name="notfind">不存在该文件</string> <string name="delete">删除文件</string> </resources>
openFileOutput(“a.txt”,Context.MODE_WORLD_READABLE)方法第一个参数是文件名,若不存在这个文件,则自动创建,第二个参数是操作模式共四种:
Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput(“itcast.txt”, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
相关文章推荐
- context.openFileoutput及文件存储,删除,读取简单用法
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- Android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput() .
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- 【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置
- SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置
- 文件存储openFileOutput和openFileInput
- Android数据存储之文件 openFileOutput & openFileInput
- Android——FileOutputStream与openFileOutput()的区别分析【第一个用于文件存储,第二个用于SD卡存储】【转】
- openFileOutput() openFileInput() 对数据进行读取和存储
- 读写内部存储的文件数openFileOutput(),openFileInput()
- 文件存储openFileOutput和openFileInput 和String.getBytes()