FileUtil.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Util
  5. {
  6. internal class FileUtil
  7. {
  8. //存储文件
  9. public static FileStream saveFile(string fileName, JSONObject node = null)
  10. {
  11. string folder = Path.GetDirectoryName(fileName);
  12. if (!Directory.Exists(folder))
  13. Directory.CreateDirectory(folder);
  14. FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
  15. if (node == null)
  16. return fs;
  17. StreamWriter writer = new StreamWriter(fs);
  18. string encodedString = node.Print(true);
  19. writer.Write(encodedString);
  20. writer.Close();
  21. return fs;
  22. }
  23. //获取path2对于path1的相对路径,自己使用,不做非法判断
  24. public static string getRelativePath(string path1, string path2)
  25. {
  26. String relativePath = "";
  27. String[] pathArr1 = path1.Split('/');
  28. String[] pathArr2 = path2.Split('/');
  29. int equalNum = 0;
  30. for (int i = 0; i < pathArr1.Length - 1; i++)
  31. {
  32. if (pathArr1[i] != pathArr2[i])
  33. break;
  34. equalNum++;
  35. }
  36. for (int j = 0; j < pathArr1.Length - equalNum - 1; j++)
  37. {
  38. relativePath += "../";
  39. }
  40. for (int j = equalNum; j < pathArr2.Length; j++)
  41. {
  42. relativePath += pathArr2[j];
  43. if (j < pathArr2.Length - 1)
  44. relativePath += "/";
  45. }
  46. return relativePath;
  47. }
  48. public static void WriteData(FileStream fs, params Int32[] datas)
  49. {
  50. foreach (Int32 data in datas)
  51. {
  52. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  53. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  54. }
  55. }
  56. private static void WriteData(FileStream fs, params long[] datas)
  57. {
  58. foreach (long data in datas)
  59. {
  60. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  61. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  62. }
  63. }
  64. public static void WriteData(FileStream fs, params byte[] datas)
  65. {
  66. foreach (byte data in datas)
  67. {
  68. fs.WriteByte(data);
  69. }
  70. }
  71. public static void WriteData(FileStream fs, params ushort[] datas)
  72. {
  73. foreach (ushort data in datas)
  74. {
  75. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  76. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  77. }
  78. }
  79. public static void WriteData(FileStream fs, params short[] datas)
  80. {
  81. foreach (short data in datas)
  82. {
  83. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  84. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  85. }
  86. }
  87. public static void WriteData(FileStream fs, params uint[] datas)
  88. {
  89. foreach (uint data in datas)
  90. {
  91. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  92. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  93. }
  94. }
  95. private static void WriteData(FileStream fs, params sbyte[] datas)
  96. {
  97. //byte[] bs = (byte[])(Array)datas;
  98. BinaryWriter s = new BinaryWriter(fs);
  99. foreach (sbyte data in datas)
  100. {
  101. //fs.WriteByte(data);
  102. s.Write(data);
  103. }
  104. }
  105. public static void WriteData(FileStream fs, params float[] datas)
  106. {
  107. foreach (float data in datas)
  108. {
  109. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  110. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  111. }
  112. }
  113. private static void WriteData(FileStream fs, params double[] datas)
  114. {
  115. foreach (double data in datas)
  116. {
  117. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  118. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  119. }
  120. }
  121. public static void WriteData(FileStream fs, params bool[] datas)
  122. {
  123. foreach (bool data in datas)
  124. {
  125. byte[] blockAddressIndexBytes = BitConverter.GetBytes(data);
  126. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  127. }
  128. }
  129. public static void WriteData(FileStream fs, string data)
  130. {
  131. byte[] blockAddressIndexBytes = UTF8Encoding.UTF8.GetBytes(data);
  132. short blocklength = (short)blockAddressIndexBytes.Length;
  133. WriteData(fs, blocklength);
  134. fs.Write(blockAddressIndexBytes, 0, blockAddressIndexBytes.Length);
  135. }
  136. }
  137. }