2

我有一个按字母顺序排序的现有 .txt 文件。我想按字母顺序将 3 个新参数插入到现有的 .txt 文件中。基本上,应该在两者之间添加新值,以便保持 .txt 文件的字母顺序。

有人可以帮我吗?我该怎么做呢?

4

4 回答 4

4

您可以将其称为“合并排序”,我可以很快想到两种方法。

  1. 一次读取一行原始文件,然后输出到一个新文件,在正确的位置添加附加内容。

  2. 将整个文件读入集合,将新条目添加到集合中,确保通过某种方式对集合进行排序(或使用固有排序的集合类型),然后将整个集合写回文件。

我不会做的是尝试在文件中打开空间并将新条目直接插入文件中。

于 2012-12-13T03:15:52.393 回答
1

这可能取决于文本文件的大小,但我会阅读文本文件:

List<string> items = GetItemsFromTextFile(); //you're going to use IOStreams for this

插入新项目。

items.add("new item 1");
items.add("new item 2");
items.add("new item 3");

解决:

items.Sort();

然后写它。

于 2012-12-13T03:16:02.090 回答
1

假设性能并不重要,您可以将文件加载到内存中,您只需执行以下操作。

var newLines = new [] { "new line one", "new line two", "new line three" };

var lines = File.ReadAllLines(filename);

lines = lines.Append(newLines).OrderBy(line => line).ToArray();

File.WriteAllLines(filename, lines);
于 2012-12-13T03:17:02.680 回答
0

您可以初始化 aStreamReader或用于File.ReadAllLines(string path, Encoding encoding)将文件内容读取到string由新行分割的数组中。然后,对该数组进行排序string,将数组中的每一个写入文件。

例子

public static void WriteToFile(string[] linesContent, string fileLocation) //Creates a void of name WriteToFile(string[] linesContent, string fileLocation) where linesContent are the lines to write and fileLocation is the target file path to write to
{
    StreamWriter _writer = new StreamWriter(fileLocation); //Initialize a new StreamWriter of name _writer to write to fileLocation
    foreach (string s in linesContent) //Get s as a string for every string in linesContent
    {
        _writer.WriteLine(s); //Write s in a new line to the file
    }
    _writer.Close(); //Close the writer
}
public static void SortFile(string fileLocation) //Creates a void of name SortFile(string fileLocation) where fileLocation is the path of the file to sort alphabetically
{ 
    string[] Lines = File.ReadAllLines(fileLocation, Encoding.UTF8); //Initializes a string array of name Lines as the content of the file splitted by Environment.NewLine
    Array.Sort(Lines); //Sorts the string array of name Lines
    WriteToFile(Lines, fileLocation); //Writes Lines to the file
}
static void Main() //Our main entry point
{
    string fileLocation = @"D:\Resources\myfile.txt"; //Initializes a new string of name fileLocation as D:\Resources\myfile.txt
    WriteToFile(new string[] { "my third line", "this is my second line", "and this is my first line" }, fileLocation); //Writes the three lines provided to fileLocation
    SortFile(fileLocation); //Sorts fileLocation
}

注意:在初始化新StreamWriter的名称_writer时,我们没有设置append为,true因为我们将用新的排序行覆盖文件
注意:您可以使用StreamReader.ReadToEnd();读取文件的所有行,将行拆分为新行,然后关闭StreamReader

例子

public static void SortFile(string fileLocation) //Creates a void of name SortFile(string fileLocation) where fileLocation is the path of the file to sort alphabetically
{ 
    StreamReader _reader = new StreamReader(fileLocation); //Initializes a new StreamReader of name _reader to read from fileLocation

    string fileContents = _reader.ReadToEnd(); //Initializes a new string of name fileContents which is the file content 
    string[] Lines = fileContents.Split(Environment.NewLine.ToCharArray()[0]); //Initializes a string array which is the output of splitting fileContents by a line

    Array.Sort(Lines); //Sorts the string array of name Lines
    _reader.Close(); //Closes the StreamReader so that the file can be accessible once again
    WriteToFile(Lines, fileLocation); //Writes Lines to the file
}

谢谢,
我希望你觉得这有帮助:)

于 2012-12-13T03:42:02.217 回答