写一个External Storage(外部存储)的代码,是参考了老罗视频中的代码稍作了些修改,在此表示感谢!!!
直接上代码。
实现数据的存,与取的类。
public class FileService {
private Context context;//传入上下文对象。
public FileService(Context context) {
this.context = context;
}
//存储一个字符串的方法。
public boolean saveContentToSdcard(String filename, String content) {
boolean tag = false;
FileOutputStream outputStream = null;//文件输出流
File file = new File(Environment.getExternalStorageDirectory(),
filename);//新建一个文件,通过getExternalStorageDirectory方法获得SD卡路径,filenam指定文件名。
//判断当前外存是否可用。
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
try {
outputStream = new FileOutputStream(file);//初始化文件流
outputStream.write(content.getBytes());//将STring型的content转换为字节数组,然后写到输出流中。
tag = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return tag;
}
//读取一个文件的方法。
public String getContentFromSdcard(String file_name) {
FileInputStream fileInputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();//这是一个字节缓冲输出流,数据保存在内存中,使用结束后不需要关闭流。
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
File file = new File(Environment.getExternalStorageDirectory(),
file_name + ".txt");
try {
fileInputStream = new FileInputStream(file);
int len = 0;
byte[] data = new byte[1024];//定义字节数组
//每次读取一个data大小的字符数组,并将其写到输出流中。
while ((len = fileInputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return outputStream.toString();// 最后将输出流转换为字符串返回。
}
}
主要用到的方法及对象是:
Environment.getExternalStorageState();
Environment.getExternalStorageDirectory();
FileInputStream/FileoutputStream
byteArrayOutputStream
Activity类
通过输入的文件名来创建文件还包括内容。
package com.example.external_storage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private EditText editText1;
private EditText editText2;
private EditText editText3;
private TextView textView;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FileService fileService = new FileService(context);
textView = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name,content;
name = editText2.getText().toString();
content=editText1.getText().toString();
fileService.saveContentToSdcard(name+".txt", content);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String file_name;
file_name=editText3.getText().toString();
String content = fileService.getContentFromSdcard(file_name);
textView.setText(content);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.external_storage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.external_storage"></instrumentation>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner"/>"
<activity
android:name="com.example.external_storage.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>