public void save(String filename, String message) {
try {
FileOutputStream fout = openFileOutput(filename, MODE_PRIVATE);
byte[] bytes = message.getBytes();
fout.write(bytes);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文件
*/
public void readFileData(String fileName) {
try {
FileInputStream fin = openFileInput(fileName);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fin.read(buffer)) != -1) {
bout.write(buffer, 0, len);
}
bout.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 往sdcard中创建文件 <!-- 在SDCard中创建与删除文件权限 --> <uses-permission
* android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!--
* 往SDCard写入数据权限 --> <uses-permission
* android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
*
* 程序必须先判断手机是否装有SDCard,并且可以进行读写
*/
public void saveMsgToSDcard(String filename, String message) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();
File saveFile = new File(sdCardDir, filename);
try {
FileOutputStream fout = new FileOutputStream(saveFile);
byte[] bytes = message.getBytes();
fout.write(bytes);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}