我有一个按字母顺序排序的现有.txt文件。我想按字母顺序在现有的.txt文件中插入3个新参数。基本上,应在两者之间添加新值,以便保持.txt文件的字母顺序。
有人可以帮帮我吗?我怎么能继续这个呢?
答案 0 :(得分:4)
你可以称之为'合并排序',我可以很快想到两种方法。
一次读取一行原始文件,然后输出到新文件,在正确的位置添加其他内容。
将整个文件读入一个集合,将新条目添加到集合中,确保通过某种方式对集合进行排序(或使用本身已排序的集合类型),然后将整个集合写回到文件。
我不会做的是尝试打开文件中的空格并将新条目直接插入文件中。
答案 1 :(得分: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();
然后写下来。
答案 2 :(得分: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);
答案 3 :(得分:0)
您可以初始化StreamReader
或使用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
}
谢谢, 我希望你觉得这很有帮助:)