代码:
/// <summary> /// 传感器ID /// </summary> public Dictionary<string, string> SensorIDDic = new Dictionary<string, string>(); public string contents= "pen,241007|bookL,240806|bookR,241000"; /// <summary> /// 获取传感器ID /// </summary> void GetSensorData() { #if UNITY_EDITOR string path = Application.persistentDataPath + "/SensorConfig.txt"; #elif UNITY_ANDROID string path = "/storage/emulated/0/SensorConfig.txt"; #endif if (!File.Exists(path)) { //文本不存在创建文本 FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8); foreach (var item in contents.Split("|")) { sw.WriteLine(item); } sw.Close(); fileStream.Close(); OnGetFileContent(path); } else { OnGetFileContent(path); } } /// <summary> /// 读取文件内容 /// </summary> /// <param name="path"></param> private void OnGetFileContent(string path) { //文件流形式读取文档 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); string str = Encoding.UTF8.GetString(bytes); str = str.TrimEnd((char[])"\r\n".ToCharArray()); //str = Regex.Replace(str, str.TrimEnd((char[])"\r\n".ToCharArray()), ""); //Debug.LogError("str::" + str); if (str != "" && str != null) { //var tempStr = str.Split(","); //foreach (var item in tempStr) //{ // SensorIDlist.Add(item); //} //foreach (var iem in SensorIDlist) //{ // Debug.LogError("SensorIDlist:" + iem); //} var str1 = str.Split("\r\n"); Debug.LogError("TEST:" + str1[0]); //foreach (var item1 in str1) { Debug.LogError("str1.Length:" + str1.Length); for (int i = 0; i < str1.Length; i++) { var ie = str1[i].Split(","); Debug.LogError("item2[0]:" + i + ":" + ie[0] + " item2[1]:" + ie[1]); SensorIDDic.Add(ie[0], ie[1]); } } //foreach (var item in SensorIDDic.Keys) //{ // Debug.LogError(item); //} } } }
void Test()
{
if (sn.Contains(AppStart.Instance.SensorIDDic["pen"]))
{
Debug.LogError("sn:" + sn+ " SensorIDDic[\"pen\"]" + AppStart.Instance.SensorIDDic["pen"]);
}
}
标签:LogError,读取,str1,Debug,str,var,path,Android,ID From: https://www.cnblogs.com/WalkingSnail/p/18408099