日期:2014-05-18 浏览次数:20958 次
try
{
StreamReader sr = new StreamReader("test.txt");
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
string[] temp = str.Split(' ');
for (int i = 0; i < temp.Length; i++)
{
if (temp[i].Length != 4)
temp[i] = temp[i].PadLeft(4, ' ');
}
str = string.Join(" ", temp);
sb.Append(str + "\n");
}
sr.Close();
string[] result = sb.ToString().Split("\n".ToCharArray());
StreamWriter sw = new StreamWriter("test.txt", false);
foreach (string s in result)
sw.WriteLine(s);
sw.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
StreamReader sr = new StreamReader(@"D:\1.txt"); using (StreamWriter sw = new StreamWriter(@"D:\2.txt", true)) { while (sr.Peek() != -1) { string[] temp = sr.ReadLine().Split(' '); for (int i = 0; i < temp.Length; i++) temp[i] = temp[i].PadLeft(4, ' '); string s = string.Join(" ", temp); sw.WriteLine(s); } } sr.Close();