流读取器中的子字符串错误
本文关键字:字符串 错误 读取 | 更新日期: 2024-10-25 21:22:33
您好,我在为 Unity3D 编写编辑器时遇到问题,我遇到了一个问题,即我正在从具有常规字符串的.txt文件中读取行,然后读取每个常规字符串(表示扩展名的类别)下方的文件扩展名。当我尝试在分配给下一行的字符串上运行子字符串时,问题就出现了。当我尝试使用任何子字符串时,文件显示为打开失败,但没有它,打开就可以了。
public bool PopulateList()
{
bool success = true;
string path = "Assets/Scripts/Editor/Extensions.txt";
sourceFile = new FileInfo("Assets/Scripts/Editor/Extensions.txt");
if (!File.Exists(path))
{
Debug.Log("File Does Not Exist");
TextWriter tw = new StreamWriter(path, true);
tw.Close();
}
string line;
ExtensionUnit anExtension;
try
{
StreamReader myStreamReader = sourceFile.OpenText();
line = myStreamReader.ReadLine();
while (!myStreamReader.EndOfStream)
{
anExtension = new ExtensionUnit();
anExtension.Categories = line;
line = myStreamReader.ReadLine();
/*if(line.Substring(0,1) == ".")
{
//Debug.Log(line.Substring(0, 1));
}*/
//Debug.Log(line.Substring(0, 1));
/*while(line.Substring(0,1) == ".")
{
anExtension.Extensions = line;
theExtensions.Add(anExtension);
//Next extension
line = myStreamReader.ReadLine();
}*/
//Empty blank space
line = myStreamReader.ReadLine();
}
myStreamReader.Close();
}
catch
{
success = false;
}
return success;
}
}
首先读取所有文件数据,然后执行所有逻辑(子字符串等)
public bool PopulateList()
{
var success = true;
var path = "Assets/Scripts/Editor/Extensions.txt";
if (File.Exists(path))
{
try
{
var fileContent = File.ReadAllLines(path);
foreach (var line in fileContent)
{
// Define what lines do you need and get needed extensions
}
}
catch (Exception ex)
{
Log(ex); // it`s better to know the reason at least
success = false;
}
}
return succes;
}
尝试在不打开文件的情况下执行此操作:
StreamReader myStreamReader = new StreamReader(sourceFile.FullName);