String fileName = "IK_XPO_mock.txt"; System.out.println(fileName); BufferedWriter fw = null; try { File file = new File("exprtDIR"+fileName); //判断目标文件所在的目录是否存在 if(!file.getParentFile().exists()) { //若是目标文件所在的目录不存在,则建立父目录 System.out.println("目标文件所在目录不存在,准备建立它!"); if(!file.getParentFile().mkdirs()) { System.out.println("建立目标文件所在目录失败!"); } } fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8")); // 指定编码格式,以避免读取时中文字符异常 //写入内容 fw.append(AudioUrl); fw.flush(); // 所有写入缓存中的内容 } catch (Exception e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } }