- 读文件
/** * 读文件 * @param jsonPath 文件所在路径 * @return */ public static String readJson(String jsonPath){ String jsonStr = ""; try { File file = new File(jsonPath); FileReader fileReader = new FileReader(file); // Reader reader = new InputStreamReader(new FileInputStream(file),"Utf-8"); Reader reader = new InputStreamReader(new FileInputStream(file)); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (Exception e) { return null; } }
- 写文件
/** * 写文件 * @param path json文件路径 * @param str json数据 * @return * @throws IOException */ public static void writeJson(String path,String str) throws IOException { FileWriter fw = new FileWriter(path); PrintWriter out = new PrintWriter(fw); out.write(str); out.println(); fw.close(); out.close(); }
- 删除文件
/** * 删除文件 * * @param filePath 文件 * @return */ public static boolean deleteFile(String filePath) { boolean flag = false; File file = new File(filePath); // 路径为文件且不为空则进行删除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; }
- 百分号工具类
/** * 百分号编码工具方法 * * @param s 需要百分号编码的字符串 * @return 百分号编码后的字符串 */ public static String percentEncode(String s) throws UnsupportedEncodingException { String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); return encode.replaceAll("\\+", "%20"); }