android把字符串内容保存到指定路径
public static void saveFile(String str) {
String filePath = null;
boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (hasSDCard) { // SD卡根目录的hello.text
filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";
} else // 系统下载缓存根目录的hello.text
filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "hello.txt";
try {
File file = new File(filePath);
if (!file.exists()) {
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(str.getBytes());
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
注意:需要在AndroidManifest.xml配置读写文件权限