读写文本文件 [英] Read and write the text file
问题描述
请为我提供以下任务的建议。
C#执行程序给出文本文件到输出文本文件
输入.txt
Col1 Col2
Id1 Val1
Id2 Val2
Id3 Val3
1d1 Val4
Id4 Val5
Id2 Val6
Idn
ouput.txt
Col1 Col2
Id1 Val1; val4
Id2 Val2; val6
Id3 Val3
Id4 Val5
Hi ,
Please provide me advise on below task.
C# Program to Execute given text file into output text file
Input.txt
Col1 Col2
Id1 Val1
Id2 Val2
Id3 Val3
1d1 Val4
Id4 Val5
Id2 Val6
Idn
ouput.txt
Col1 Col2
Id1 Val1;val4
Id2 Val2;val6
Id3 Val3
Id4 Val5
推荐答案
您好,
试试这个:
Hi,
Try this:
// For store input data;
var data = new Dictionary<string, List<string>>();
// Load data from file
using (var streamReader = new StreamReader(@"D:\input.txt"))
{
while(!streamReader.EndOfStream)
{
var line = streamReader.ReadLine();
var items = line.Split(" ".ToCharArray());
var id = items[0];
var value = items[1];
if (!data.ContainsKey(id))
{
data.Add(id, new List<string>());
}
data[id].Add(value);
}
}
// Sava file
using (var streamWriter = new StreamWriter(@"D:\output.txt"))
{
foreach (var key in data.Keys)
{
var values = string.Join(";", data[key]);
streamWriter.WriteLine(string.Format("{0} {1}", key, values));
}
}
一些教育链接:
http://msdn.microsoft.com/en-us/library/ vstudio / system.io.streamreader%28v = vs.90%29 [ ^ ]
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter%28v=vs.90 %29 [ ^ ]
http://msdn.microsoft.com/en-us/library/vstudio/xfhwa508%28v=vs.100%29.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/system.collections%28v=vs.110%29.aspx [ ^ ]
干杯!
Some links for education:
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader%28v=vs.90%29[^]
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter%28v=vs.90%29[^]
http://msdn.microsoft.com/en-us/library/vstudio/xfhwa508%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.collections%28v=vs.110%29.aspx[^]
Cheers!
嗨请试试这个
Hi Please try this
string[] lines = System.IO.File.ReadAllLines(@"E:\Test\Input.txt");
var r = from x in lines.Select(x => x.Split('\t')).OrderBy(x=>x[0])
group x by x[0] into p
select p;
string Value = string.Empty;
string Key = string.Empty;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\Test\Output.txt"))
{
foreach (var n in r)
{
Console.WriteLine(n.Key);
Key = n.Key;
foreach (var l in n)
{
Value = Value + l[1] + ";";
}
file.WriteLine(Key + " " + Value);
Value = "";
}
}
我找到了一个解决方案。请找到下面的代码。
Hi ,
I found one solution . Please find the below code.
string[] lines = System.IO.File.ReadAllLines(@"input.txt");
string fnString = "", keyStr = "";
ArrayList arLst = new ArrayList();
arLst.Add("Col1 Col2");
for (int i = 1; i < lines.Count(); i++)
{
fnString = ""; keyStr = "";
if (lines[i].Trim() != "")
{
string[] arrRow = lines[i].Split(' ');
for (int j = 1; j < lines.Count(); j++)
{
if (lines[j].Trim() != "")
{
string[] arrRow1 = lines[j].Split(' ');
keyStr = arrRow[0];
if (arrRow[0].Equals(arrRow1[0]))
{
fnString += arrRow1[1] + ";";
lines[j] = "";
}
}
}
if (fnString.EndsWith(";"))
fnString = fnString.Substring(0, fnString.Length - 1);
arLst.Add(keyStr + ' ' + fnString);
}
}
using (var streamWriter = new StreamWriter(@"output.txt"))
{
for (int i = 0; i < arLst.Count; i++)
{
streamWriter.WriteLine(arLst[i]);
}
streamWriter.Close();
}
这篇关于读写文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!