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

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

	public static void saveFile(String str) {
		String filePath = null;
		boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
		if (hasSDCard) {
			filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";
		} else
			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();
		}


你可能感兴趣的:(android把字符串内容保存到指定路径)