android把字符串内容保存到指定路径

来源:互联网 发布:基础网络知识书籍 编辑:程序博客网 时间:2024/06/07 08:18

   android把字符串内容保存到指定路径

public static void saveFile(String str) {String filePath = null;boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);if (hasSDCard) { // SD卡根目录的hello.textfilePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";} else  // 系统下载缓存根目录的hello.textfilePath = 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配置读写文件权限