将数组拆分为多个数组

时间:2014-07-16 08:59:35

标签: c# file-io arraylist

我有一个包含如下内容的文本文件:

01 Dir1
abcd
efg
hij
klm
nop
qrs
tuv
wxy
zab
yxw
vut
dcb

02 Dir2
abcd
efg
hij
klm
nop
qrs
tuv
wxy
zab
yxw
vut
dcb

我得到一个通过读取文件创建的数组:

string[] lines = File.ReadAllLines(path);

这给了我一个数组将所有条目包括空的。

文本文件背后的想法是有一个文件夹和该文件夹中的文件。因此,“01 Dir1”是一个文件夹,之后的每一行直到空行为文件。

我正在尝试的是有一个数组列表,所以从上面的示例中,列表中将有两个数组,一个从“01 Dir1”开始直到空条目,另一个从“02 Dir2”开始直到端。

我可以循环遍历初始数组并为每个目录创建列表,但还有另一种方法吗?

该方法的问题在于它最终会在内存中的不同集合中具有相同的数据,一个来自ReadAllLines数组,另一个来自它。

4 个答案:

答案 0 :(得分:1)

你不喜欢以下内容吗?

这将逐行读取,您可以决定如何处理它。 所以你不会得到一个包含所有条目的大字符串数组。 这里的结果将是一个包含字符串数组并跳过空行的列表。

//This will be the resulting list of arrays
var fileArrayList = new List<string[]>();
var filereader = new System.IO.StreamReader("yourfile");
var tmpArr = new List<string>();
var line = "";
while((line = filereader.ReadLine()) != null)
{
   if(String.IsNullOrEmpty(line){
       //put this array to our list
       fileArrayList.Add(tmpArr.ToArray());
       //clean the temp one
       tmpArr.Clear();
   }else
   {
       tmpArr.Add(line);
   }
}
//add the last entry
fileArrayList.Add(tmpArr.ToArray());
tmpArr = null;
//close the stream
filereader.Close();

答案 1 :(得分:0)

通过这种方式,您可以拥有Dictionary,其中包含目录名称作为键,文件名List<string>作为值。

我没有处理特殊情况

Dictionary<string, List<string>> data = new Dictionary<string,List<string>>();
using (StreamReader reader = File.OpenText(filename))
{
    while (!reader.EndOfStream)
    {
        string dirName = reader.ReadLine();
        List<string> currentFiles = new List<string>();
        data.Add(dirName, currentFiles);
        string fileName;
        while (!reader.EndOfStream && (fileName = reader.ReadLine()).Trim() != "")
        {
            currentFiles.Add(fileName);
        }
    }
}

答案 2 :(得分:0)

list<string> listBeforeEmpty = new list<string>();
list<string> listAfterEmpty = new list<string>();

//Indicates whether the loop is before the empty line or not
bool isEmptyLineFound = false;

 for (int i = 0; i < lines.Length; i++)
    {

     //If the empty line was found
     if (lines[i].CompareTo(@"") == 0)
            {
                isEmptyLineFound = true;
            }

    //Add the line to the right list
     if (isEmptyLineFound == false) 
          listBeforeEmpty .Add(lines[i]);
     else listAfterEmpty .Add(lines[i]);

    }

答案 3 :(得分:0)

我决定去玩这个很有趣。这就是我提出的,它应该工作:

    private static string[] _arr = new string[] // The stuff you read from file.

    private static List<string[]> _arrays = new List<string[]>(); 

        while (_arr.Length > 0)
        {
            var a = _arr.TakeWhile(s =>
            {
                var li = _arr.ToList();
                return 
                    s != string.Empty // Take it if the string isn't empty.
                    || (s == string.Empty && li.IndexOf(s) == 0) // Or if it's empty, but it's the first element in the array (it's the next one after we've just finished a batch)
                    || li.IndexOf(s) == _arr.Length; // And take it if it's the last element (in the case of an empty string as the last element)
            }).ToArray();

            _arrays.Add(a);

            // Re-create the array to only include the stuff we haven't solved yet.
            var l = _arr.ToList();
            l.RemoveRange(0, a.Length);
            _arr = l.ToArray();
        }

它有点hackish并且可以改进,但它应该是你可以使用的东西。

另外,您应该使用另一种方法来读取文件并在读取文件时确定这些单独的数组。它效率更高,也可能更容易。

相关问题