1、明明安卓中没问题,但是就是出现疑似某个文件加载不了的问题,解决方法是转成无bom的utf-8文件,以下是抄自网络的文件检测和修改成无bom utf-8格式代码
1 ///2 /// 遍历检查是否存在并非UTF-8无Bom模式的lua代码 3 /// 4 /// 5 static void CheckAnd2Utf8WithoutBom(string sSourcePath) 6 7 { 8 //在指定目录及子目录下查找文件,在list中列出子目录及文件 9 10 DirectoryInfo Dir = new DirectoryInfo(sSourcePath); 11 12 DirectoryInfo[] DirSub = Dir.GetDirectories(); 13 14 if (DirSub.Length <= 0) 15 { 16 17 foreach (FileInfo f in Dir.GetFiles()) //查找文件 18 19 { 20 if (!f.Name.EndsWith(".lua")) 21 { 22 continue; 23 } 24 FileStream fs = File.OpenRead(f.FullName); 25 26 byte[] data = new byte[fs.Length]; 27 28 fs.Read(data, 0, data.Length); 29 fs.Close(); 30 31 if (!IsUTF8Bytes(data)) 32 { 33 //转成无Bom格式 34 string path = f.FullName.Replace("\\", "/"); 35 StreamWriter docWriter; 36 var utf8WithoutBom = new UTF8Encoding(false); 37 docWriter = new StreamWriter(path, false, utf8WithoutBom); 38 var type = GetType(data); 39 docWriter.Write(type.GetString(data)); 40 docWriter.Close(); 41 } 42 } 43 44 } 45 46 foreach (DirectoryInfo d in DirSub)//查找子目录 47 48 { 49 CheckAnd2Utf8WithoutBom(d.FullName, ref err); 50 } 51 52 } 53 54 ///55 /// 通过给定的文件流,判断文件的编码类型 56 /// 57 /// 文件流 58 ///文件的编码类型 59 public static Encoding GetType(byte[] data) 60 { 61 //byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 }; 62 //byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 }; 63 //byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM 64 Encoding reVal = Encoding.Default; 65 //BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default); 66 //int i; 67 //int.TryParse(fs.Length.ToString(), out i); 68 //byte[] ss = r.ReadBytes(i); 69 if (IsUTF8Bytes(data) || (data.Length > 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)) 70 { 71 reVal = Encoding.UTF8; 72 } 73 else if (data.Length > 3 && data[0] == 0xFE && data[1] == 0xFF && data[2] == 0x00) 74 { 75 reVal = Encoding.BigEndianUnicode; 76 } 77 else if (data.Length > 3 && data[0] == 0xFF && data[1] == 0xFE && data[2] == 0x41) 78 { 79 reVal = Encoding.Unicode; 80 } 81 return reVal; 82 83 } 84 85 ///86 /// 判断是否是不带 BOM 的 UTF8 格式 87 /// 88 /// 89 ///90 private static bool IsUTF8Bytes(byte[] data) 91 { 92 int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 93 byte curByte; //当前分析的字节. 94 for (int i = 0; i < data.Length; i++) 95 { 96 curByte = data[i]; 97 if (charByteCounter == 1) 98 { 99 if (curByte >= 0x80)100 {101 //判断当前102 while (((curByte <<= 1) & 0x80) != 0)103 {104 charByteCounter++;105 }106 //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 107 if (charByteCounter == 1 || charByteCounter > 6)108 {109 return false;110 }111 }112 }113 else114 {115 //若是UTF-8 此时第一位必须为1116 if ((curByte & 0xC0) != 0x80)117 {118 return false;119 }120 charByteCounter--;121 }122 }123 if (charByteCounter > 1)124 {125 throw new Exception("非预期的byte格式");126 }127 return true;128 }
2、