351 /**2 * FileReader适用于字符流的读取3 * 4 * @param fileName5 * @param content6 */7 private static String fileReaderFromFile(String fileName) {89 FileReader fileReader = null;10 StringBuffer sb = null;11 try {12 fileReader = new FileReader(fileName);13 char[] buf = new char[1024];14 // read(char [])返回读到的字符个数15 int length = 0;16 sb = new StringBuffer();17 // 读取文件并把它存入buf中,用num返回读到字符的个数,一直读到结尾18 while ((length = fileReader.read(buf)) != -1) {19 // new String(字符串,开始位置,结尾位置)20 System.out.print((new String(buf, 0, length)));// 字符数组里仍有空白没有读入的位置,所以不要了21 sb.append(new String(buf, 0, length));22 }23 } catch (IOException e) {24 e.printStackTrace();25 } finally {26 try {27 if (fileReader != null) {28 fileReader.close();29 }30 } catch (IOException e) {31 e.printStackTrace();32 }33 }34 return sb.toString();35 }xxxxxxxxxx1 /**2 * FileWriter适用于字符流的写出3 * 4 * @param fileName5 * @param content6 */7 private static void fileWriterToFile(String fileName, String content) {89 File file = new File(fileName);// 新建一个文件对象,如果不存在则创建一个该文件10 FileWriter fw = null;11 try {12 fw = new FileWriter(file);13 fw.write(content);// 将字符串写入到指定的路径下的文件中14 } catch (IOException e) {15 e.printStackTrace();16 } finally {17 try {18 if (fw != null) {19 fw.close();20 }21 } catch (IOException e) {22 e.printStackTrace();23 }24 }25 }xxxxxxxxxx1 /**2 * FileInputStream适用于读取原始字节数据,如图像3 * 4 * @param fileName5 */6 public static String InputStreamReadFile(String fileName) {7 File file = new File(fileName);8 InputStream fileInputStream = null;9 StringBuffer sb = null;10 // 准备好一个输入的对象11 try {12 fileInputStream = new FileInputStream(file);13 byte buf[] = new byte[1024];14 // 所有的内容都读到此数组之中15 int length = 0;16 sb = new StringBuffer();17 // 循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。18 // 当文件读取到结尾时返回 -1,循环结束。19 while ((length = fileInputStream.read(buf)) != -1) {20 System.out.print(new String(buf, 0, length));21 sb.append(new String(buf, 0, length));22 }23 } catch (Exception e) {24 e.printStackTrace();25 } finally {26 try {27 if (fileInputStream != null) {28 fileInputStream.close();29 }30 } catch (IOException e) {31 e.printStackTrace();32 }33 }34 return sb.toString();35 }311 /**2 * FileOutputStream适用于写入原始字节数据,如图像3 * 4 * @param fileName5 * @param content6 */7 public static void OutputStreamToFile(String fileName, String content) {8 // 声明File对象9 File file = new File(fileName);10 // 准备好一个输出的对象11 OutputStream out = null;12 // 通过对象多态性,进行实例化13 try {14 out = new FileOutputStream(file);15 // 只能输出byte数组,所以将字符串变为byte数组16 byte b[] = content.getBytes();17 // 将内容输出,18 out.write(b);19 } catch (Exception e) {20 // TODO Auto-generated catch block21 e.printStackTrace();22 } finally {23 try {24 if (out != null) {25 out.close();26 }27 } catch (IOException e) {28 e.printStackTrace();29 }30 }31 }201 /**2 * 获取json字段数据3 */4 public static Object getValue(JSONObject jsonObj, String key) {5 if (jsonObj == null || jsonObj.isEmpty() || StringUtils.isBlank(key)) {6 return null;7 }8 9 if(jsonObj.containsKey(key)){10 return jsonObj.get(key);11 }12 Set<String> keys = jsonObj.keySet();13 for (String curKey : keys) {14 if (curKey.equalsIgnoreCase(key)) {15 return jsonObj.get(curKey);16 }17 }1819 return null;20 }391 /**2 * 使用传统io stream 下载文件(下载歌曲文件)3 * 4 * @param url5 * @param saveDir6 * @param fileName7 */8 public static void downloadFile(String url, String saveDir, String fileName) {9 BufferedOutputStream bos = null;10 InputStream is = null;11 try {12 byte[] buff = new byte[8192];13 is = new URL(url).openStream();14 File file = new File(saveDir, fileName);15 file.getParentFile().mkdirs();16 bos = new BufferedOutputStream(new FileOutputStream(file));17 int count = 0;18 while ((count = is.read(buff)) != -1) {19 bos.write(buff, 0, count);20 }21 } catch (IOException e) {22 e.printStackTrace();23 } finally {24 if (is != null) {25 try {26 is.close();27 } catch (IOException e) {28 e.printStackTrace();29 }30 }31 if (bos != null) {32 try {33 bos.close();34 } catch (IOException e) {35 e.printStackTrace();36 }37 }38 }39 }291 /**2 * 去除重复字符3 * 4 * @param text5 * @param str6 * @return7 */8 public static String removeRepeatChar(String text, String str) {9 if (text == null) {10 return "";11 }12 StringBuffer sb = new StringBuffer();13 int i = 0;14 int len = text.length();15 while (i < len) {16 char c = str.charAt(0);17 sb.append(text.charAt(i));18 i++;19 boolean fg = true;20 while (i < len && text.charAt(i) == c) {// 这个是如果这两个值相等,就让i+1取下一个21 if (fg == true) {22 sb.append(c);23 }24 fg = false;25 i++;26 }27 }28 return sb.toString();29 }131 /**2 * 判断是否只有一个括号3 */4 public static boolean isOneKuohao(String text) {5 if (text.contains("(")) {6 text = text.replaceFirst("[(]", "");7 }8 if (text.contains("(")) {9 return false;10 } else {11 return true;12 }13 }261 /**2 * 删除中文3 * @param value4 * @return result5 */6 public static String removeChinese(String value) {78 if (value == null || value.isEmpty()) {9 return value;10 }11 Pattern pat = Pattern.compile("[0-9]");12 Matcher mat = pat.matcher(value);13 if (mat.find()) {14 Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");15 Matcher matcher = pattern.matcher(value);16 String result = matcher.replaceAll("");17 return result;18 }19 return value;20 }21 /**22 * 删除中文223 * 24 */25 text = text.replaceAll("[\\u4e00-\\u9fa5]", "");26 261 /**2 * 删除数字3 * 4 * @param value5 * @return result6 */7 public static String removeDigital(String value) {89 if (value == null || value.isEmpty()) {10 return value;11 }12 Pattern pat = Pattern.compile("[0-9]");13 Matcher mat = pat.matcher(value);14 if (mat.find()) {15 Pattern pattern = Pattern.compile("[\\d]");16 Matcher matcher = pattern.matcher(value);17 String result = matcher.replaceAll("");18 return result;19 }20 return value;21 }22 /**23 * 删除数字224 * 25 */26 text = text.replaceAll("[0-9]", "");291 /**2 * 取两个文本之间的文本值3 * 4 * @param text5 * @param left6 * @param right7 * @return result8 */9 public static String getSubString(String text, String left, String right) {10 String result = "";11 int zLen;12 if (left == null || left.isEmpty()) {13 zLen = 0;14 } else {15 zLen = text.indexOf(left);16 if (zLen > -1) {17 zLen += left.length();18 } else {19 zLen = 0;20 }21 }22 int yLen = text.indexOf(right, zLen);23 if (yLen < 0 || right == null || right.isEmpty()) {24 yLen = text.length();25 }26 result = text.substring(zLen, yLen);27 return result;28 }29}
x
801 /**2 * 全角转半角3 * 4 * @param str 要转换的全角字符串5 * @return 半角字符串6 */7 public static String toHalfAngle(String str) {89 if (str == null || str.isEmpty()) {10 return str;11 }12 char[] c = str.toCharArray();13 for (int i = 0; i < c.length; i++) {14 if (c[i] == 12288) {// 全角空格15 c[i] = (char) 32;16 } else if (c[i] > 65280 && c[i] < 65375) {// 其他全角字符17 c[i] = (char) (c[i] - 65248);18 }19 }20 return String.valueOf(c);21 }2223 /**24 * 半角转全角(未测试)25 * 26 * @param str 要转换的半角字符串27 * @return 全角字符串28 */29 public static String toFullAngle(String str) {3031 if (str == null || str.isEmpty()) {32 return str;33 }34 char[] c = str.toCharArray();35 for (int i = 0; i < c.length; i++) {36 if (c[i] = (char) 32) {// 全角空格37 c[i] == 12288;38 } else if (c[i] < 65280 || c[i] > 65375) {// 其他全角字符39 c[i] = (char) (c[i] + 65248);40 }41 }42 return String.valueOf(c);43 }