例如,我有一个txt文件,内容为:
12 345 45
2342 234 45 2 2 45345
234 546 34 3 45 65 765
12 23 434 34 56 76 5
我想在所有数字之间插入逗号,在每行的开头添加左括号,在每行的末尾添加右括号。所以在编辑之后它应该是:
{12, 345, 45}
{2342, 234, 45, 2, 2, 45345}
{234, 546, 34, 3, 45, 65, 765}
{12, 23, 434, 34, 56, 76, 5}
我该怎么做?
答案 0 :(得分:18)
添加了一些LINQ以获得乐趣和利润(优化空间;)):
System.IO.File.WriteAllLines(
"outfilename.txt",
System.IO.File.ReadAllLines("infilename.txt").Select(line =>
"{" +
string.Join(", ",
line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
) + "}"
).ToArray()
);
答案 1 :(得分:13)
这样的事情:(未经测试)
string filename = @"c:\yourfilename.txt";
StringBuilder result = new StringBuilder();
if (System.IO.File.Exists(filename))
{
using (StreamReader streamReader = new StreamReader(filename))
{
String line;
while ((line = streamReader.ReadLine()) != null)
{
string newLine = String.Concat("{", line, "}", Environment.NewLine);
newLine = newLine.Replace(" ", ", ");
result.Append(newLine);
}
}
}
using (FileStream fileStream = new FileStream(filename , fileMode, fileAccess))
{
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.Write(result);
streamWriter.Close();
fileStream.Close();
}
答案 2 :(得分:5)
你应该首先处理逻辑,而不是直接要求人们为你提供。至于读/写文件,请转到:
//write
FileStream fs = new FileStream("file_name", FileMode.Create);
StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
w.WriteLine("text_to_write");
w.Flush();
w.Close();
fs.Close();
//read
fs = new FileStream("file_name", FileMode.Open);
StreamReader r = new StreamReader(fs, Encoding.UTF8);
Console.WriteLine(r.ReadLine());
r.Close();
fs.Close();
答案 3 :(得分:3)
您需要使用FileStream
类打开文件,使用StreamReader
类从文件中读取,使用StreamWriter
类写回文件。
您可以像这样创建FileStream
:
FileStream file = new FileStream("FileName", FileMode.Open, FileAccess.ReadWrite);
然后将FileStream
包裹在StreamReader
:
StreamReader reader = new StreamReader(file);
然后,读入每一行并进行字符串处理(添加逗号和括号):
while(reader.EndOfFile)
{
string currentLine = reader.ReadLine();
// do your string processing here and save the result somewhere
}
最后,将FileStream
打包在StreamWriter
中,然后将修改后的字符串写回文件:
StreamWriter writer = new StreamWriter(file);
// Write your content here
writer.Write("my content");
使用它们后别忘了关闭你的溪流。
reader.Close();
writer.Close();
file.Close();
答案 4 :(得分:3)
阅读每一行。
在字符串前面和
之后添加一个括号然后用“,”(逗号和空格)替换空格“”
答案 5 :(得分:3)
编辑以添加如何修改sLine。 (没有测试,但我很确定它会工作得很好)
StreamReader sr = new StreamReader("path/to/file.txt");
StreamWriter sw = new StreamWriter("path/to/outfile.txt");
string sLine = sr.ReadLine();
for (; sLine != null; sLine = sr.ReadLine() )
{
sLine = "{" + sLine.Replace(" ", ", ") + "}";
sw.WriteLine(sLine);
}
答案 6 :(得分:3)
string [] lines = File.ReadAllLines("input.txt");
var processed = lines.Select(line => string.Format("{{{0}}}", line.Replace(" ", ", ")));
File.WriteAllLines("output.txt",processed.ToArray());
答案 7 :(得分:1)
或者,正如waqasahmed所说的那样,只需一行就行。
另请参阅:http://www.csharphelp.com/archives/archive24.html
此外,这听起来像是一个家庭作业问题。也许我们应该有一个“家庭作业”标签?
答案 8 :(得分:1)
最后我使用了第二个文件而不是编辑第一个文件:
TextReader reader = new StreamReader("triangle.txt");
TextWriter writer = new StreamWriter("triangle2.txt");
for (; ; )
{
string s = reader.ReadLine();
if (s == null)
break;
s = s.Replace(" ", ", ");
s = "{" + s + "},";
writer.WriteLine(s);
}