您的位置:首页 > 移动开发 > Android开发

Android文件保存与读取

2015-04-10 19:21 453 查看
//MainActivity.class
package com.example.file;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.example.Service.FileService;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_save = (Button) this.findViewById(R.id.button_save);
button_save.setOnClickListener(this);
Button button_open = (Button) this.findViewById(R.id.button_open);
button_open.setOnClickListener(this);
}

@Override
public void onClick(View v) {
EditText filenameText = (EditText) this.findViewById(R.id.filename);
String filename = filenameText.getText().toString();
FileService service = new FileService(getApplicationContext());
switch (v.getId()) {
case R.id.button_save:
EditText filecontentText = (EditText) this
.findViewById(R.id.filecontent);
String filecontent = filecontentText.getText().toString();
try {
service.save(filename, filecontent);
Toast.makeText(getApplicationContext(), R.string.success, 1)
.show();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), R.string.fail, 1)
.show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.fail, 1)
.show();
e.printStackTrace();
}
break;
case R.id.button_open:
try {
String content = service.read(filename);
TextView contentText = (TextView) this
.findViewById(R.id.opencontent);
contentText.setText(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

//FileService.java
package com.example.Service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileService {
private Context context;

/*   保存文件
*   @param filename 文件名称
* 	 @param filecontent 文件内容
*/

public void save(String filename, String filecontent) throws IOException {
//私有操作模式:创建出来的文件只能被本应用访问,另外采用私有操作模式创建的文件,写入文件内容会覆盖
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(filecontent.getBytes());
outStream.close();
}

public FileService(Context context) {
this.context = context;
}

/*   读取文件
*   @param filename 文件名称
*   @return 文件内容
* 	 @throws Exception
*/
public String read(String filename) throws IOException{
FileInputStream inStream = context.openFileInput(filename);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length=0;
while((length = inStream.read(buffer))!=-1){
outStream.write(buffer, 0, length);
}
byte[] data = outStream.toByteArray();
return new String(data);
}
}

//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<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/filecontent" />
<EditText
android:minLines="3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/filecontent"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/button_open"
android:id="@+id/button_open"
android:layout_marginLeft="10dp"/>

<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/button_save"
android:id="@+id/button_save"
android:layout_toRightOf="@id/button_open"/>

</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/readcontent" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/opencontent"/>
</LinearLayout>

//strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">文件操作</string>
<string name="filecontent">文件内容</string>
<string name="filename">文件名称</string>
<string name="button_save">保存</string>
<string name="action_settings">Settings</string>
<string name="success">保存成功</string>
<string name="fail">保存失败</string>
<string name="button_open">打开</string>
<string name="readcontent">读取的文件内容</string>
</resources>

运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: