我有一个包含以下条目的文本文件:
676874463
676844967
676796379
676789975
676760851
676736748
我想阅读每个条目并将数据附加到它,如下所示:
237676874463#
237676844967#
237676796379#
237676789975#
237676760851#
237676736748#
即,在每个条目前添加237
,在每个条目末尾添加#
。
到目前为止,这是我的代码:
public class Program
{
public string fileDoc = @"c:\Users\UNICS\Desktop\Bulk SMS\BafutFormattedWithoutDuplicates.txt";
StreamReader reader = new StreamReader(fileDoc );
while ((inputline = reader.ReadLine()) != null)
{
// remove duplicates here
}
任何人都可以指导我这样做吗?
答案 0 :(得分:0)
我正在使用StringBuilder并创建一个新文件,我保留您想要查看的结果。我是从BIDS(Bussiness Intelligence Development Studio)做的,但它是c#所以它适合你。
using System;
using System.IO;
using System.Text;
namespace ST_d1578e806c274e419063843a63f76441.csproj
{
public void Main()
{
String SomeFileName = @"C:\Users\unmsm_02\Desktop\ExampleTextFile.txt"; // Main FILE
String AuxFileName = @"C:\Users\unmsm_02\Desktop\ResultTextFile.txt"; // File wher eis the result string
using (FileStream fs = File.Open(SomeFileName, FileMode.Open))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
String line;
while (sr.EndOfStream == false)
{
line = sr.ReadLine();
if (line.Length > 1)
{
StringBuilder sb = new StringBuilder();
sb.Insert(0, "237");
sb.Insert(3, line);
sb.Insert(line.Length + 3, "#");
String line_Aux = sb.ToString();
File.AppendAllText(AuxFileName, line_Aux + "\r\n");
}
}
Dts.TaskResult = (int)ScriptResults.Success; //FORGET ABOUT THIS, its from the BIDS enviroment, just use the above, and it should work
}
}
}
}
StringBuilder 在这里非常有用,您可以使用Insert方法创建一个String,第一个参数是您要插入的位置,第二个参数是您想要插入的String。使用 insert()后,将其转换为String,在我的情况下将其保存在 line_Aux 上,这是将保存在新文件中的行。< / p>
希望它有所帮助!
答案 1 :(得分:0)
我考虑将其读入一个数组并将其循环以进行编辑。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace EditText
{
class Program
{
static void Main(string[] args)
{
//Import text from file to an array.
string[] myArray = File.ReadAllLines("text1.txt");
//This is what will be written into the text file.
string myFileText = "";
//Loop through the array
for(int i = 0; i<myArray.Length; i++)
{
//Du the change you want here
string myString = "237" + myArray[i] + "#";
//Add the result to the string you will write to the file
myFileText += myString;
myFileText += Environment.NewLine;
}
//Write the text to the file.
File.WriteAllText("text1.txt", myFileText);
//Done
}
}
}
答案 2 :(得分:-1)
试试这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication43
{
class Program
{
const string IN_FILENAME = @"c:\temp\in.txt";
const string OUT_FILENAME = @"c:\temp\out.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(IN_FILENAME);
StreamWriter writer = new StreamWriter(OUT_FILENAME);
string inputline = "";
while ((inputline = reader.ReadLine()) != null)
{
writer.WriteLine("237" + inputline + "#");
}
writer.Flush();
writer.Close();
reader.Close();
}
}
}