CommonMethod.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.ruoyi.Common;
  2. import com.sun.jna.Pointer;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.charset.Charset;
  8. import java.nio.charset.StandardCharsets;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.Scanner;
  12. public class CommonMethod {
  13. public static void WriteBuffToPointer(byte[] byData, Pointer pInBuffer) {
  14. pInBuffer.write(0, byData, 0, byData.length);
  15. }
  16. public static String byteToString(byte[] bytes) {
  17. if (null == bytes || bytes.length == 0) {
  18. return "";
  19. }
  20. int iLengthOfBytes = 0;
  21. for (byte st : bytes) {
  22. if (st != 0) {
  23. iLengthOfBytes++;
  24. } else
  25. break;
  26. }
  27. String strContent = "";
  28. try {
  29. strContent = new String(bytes, 0, iLengthOfBytes, "UTF-8");
  30. } catch (UnsupportedEncodingException e) {
  31. e.printStackTrace();
  32. }
  33. return strContent;
  34. }
  35. /**
  36. * utf8字节数组转gbk字节数组
  37. *
  38. * @param utf8Bytes
  39. * @return
  40. */
  41. public static byte[] UTF8toGBK(byte[] utf8Bytes) {
  42. String utf8Str = new String(utf8Bytes, StandardCharsets.UTF_8);
  43. byte[] gbkBytes = utf8Str.getBytes(Charset.forName("GBK"));
  44. return gbkBytes;
  45. }
  46. /**
  47. * utf8字节数组转gbk字符串
  48. *
  49. * @param utf8Bytes
  50. * @return
  51. */
  52. public static String UTF8toGBKStr(byte[] utf8Bytes) {
  53. return new String(UTF8toGBK(utf8Bytes), Charset.forName("GBK"));
  54. }
  55. /**
  56. * 获取resource文件夹下的文件绝对路径
  57. *
  58. * @param filePath 文件相对于resources文件夹的相对路径, 格式描述举例为 conf/XX/XX.json
  59. * @return
  60. */
  61. public static String getResFileAbsPath(String filePath) {
  62. if (filePath == null) {
  63. throw new RuntimeException("filePath null error!");
  64. }
  65. return System.getProperty("user.dir") + "\\ruoyi-admin\\resources\\" + filePath;
  66. }
  67. /**
  68. * 获取控制台的一行输入
  69. *
  70. * @param inputTip 输入提示
  71. * @return 控制台的输入信息
  72. */
  73. public static String getConsoleInput(String inputTip) {
  74. System.out.println(inputTip);
  75. Scanner scanner = new Scanner(System.in);
  76. return scanner.nextLine();
  77. }
  78. /**
  79. * 输出信息到文件中
  80. *
  81. * @param fileName 文件名
  82. * @param postFix 文件后缀
  83. * @param fileContent 文件内容
  84. */
  85. public static void outputToFile(String fileName, String postFix, String fileContent) {
  86. SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss_SSS");
  87. String folder = System.getProperty("user.dir") + "\\outputFiles\\event\\";
  88. File directory = new File(folder);
  89. if (!directory.exists()) {
  90. boolean created = directory.mkdirs();
  91. if (!created) {
  92. System.out.println(folder + "_文件夹创建失败!");
  93. }
  94. }
  95. String filePath = folder + fileName + "_" + format.format(new Date()) + postFix;
  96. try {
  97. FileOutputStream fos = new FileOutputStream(filePath);
  98. fos.write(fileContent.getBytes());
  99. fos.close();
  100. } catch (IOException e) {
  101. System.out.println("输出到文件出现异常:" + e.getMessage());
  102. }
  103. }
  104. }