1.在AndroidManifest文件下添加SDCard的读写权限
view plain
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.文件操作的各种模式如下代码:(注意通过getExternalStorageDirectory方法获取SDCard的文件路径)
view plain
package com.hoo.file;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Environment;
public class FileService
{
private Context context;
public FileService(Context context)
{
this.context = context;
}
/**
* 读取文件的内容
* @param filename 文件名称
* @return
* @throws Exception
*/
public String readFile(String filename) throws Exception
{
//获得输入流
FileInputStream inStream = context.openFileInput(filename);
//new一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
//使用ByteArrayOutputStream类来处理输出流
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1)
{
//写入数据
outStream.write(buffer, 0, len);
}
//得到文件的二进制数据
byte[] data = outStream.toByteArray();
//关闭流
outStream.close();
inStream.close();
return new String(data);
}
/**
* 以默认私有方式保存文件内容至SDCard中
* @param filename
* @param content
* @throws Exception
*/
public void saveToSDCard(String filename, String content) throws Exception
{
//通过getExternalStorageDirectory方法获取SDCard的文件路径
File file = new File(Environment.getExternalStorageDirectory(), filename);
//获取输出流
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 以默认私有方式保存文件内容,存放在手机存储空间中
* @param filename
* @param content
* @throws Exception
*/
public void save(String filename, String content) throws Exception
{
//
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 以追加的方式保存文件内容
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveAppend(String filename, String content) throws Exception
{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 以允许其他应用从该文件中读取内容的方式保存文件(Context.MODE_WORLD_READABLE)
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveReadable(String filename, String content) throws Exception
{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 以允许其他应用往该文件写入内容的方式保存文件
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveWriteable(String filename, String content) throws Exception
{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 以允许其他应用对该文件读和写的方式保存文件(MODE_WORLD_READABLE与MODE_WORLD_WRITEABLE)
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveRW(String filename, String content) throws Exception
{
FileOutputStream outStream = context.openFileOutput(filename,
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
//Context.MODE_WORLD_READABLE(1) + Context.MODE_WORLD_WRITEABLE(2),其实可用3替代
outStream.write(content.getBytes());
outStream.close();
}
}
3.写入数据前判断sdcard是否存在于手机上,是否有写保护
view plain
package com.hoo.file;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
private static final String TAG = "MainActivity";
private FileService fileService;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fileService = new FileService(this);
//使用下面的方法可以快速获取当前文件夹的位置,
//这样可以在后面追加路径从而避免使用绝对路径
//File filedir = this.getFilesDir();
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//获取EditText中的内容
EditText filenameText = (EditText) findViewById(R.id.filename);
EditText contentText = (EditText) findViewById(R.id.filecontent);
String filename = filenameText.getText().toString();
String content = contentText.getText().toString();
try
{
//使用通常文件保存方式,默认保存在data/data/包名/file/XXX里面
//fileService.save(filename, content);
//判断sdcard是否存在于手机上而且没有写保护
//Android2.2版本以后sdcard的路径在mnt/sdcard,2.2之前在/sdcard
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//保存到SDCard中
fileService.saveToSDCard(filename, content);
//提示保存成功
Toast.makeText(MainActivity.this, R.string.success, 1).show();
}
else
{
//提示保存失败
Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show();
}
}
catch (Exception e)
{
Log.e(TAG, e.toString());
Toast.makeText(MainActivity.this, R.string.error, 1).show();
}
}
});
}