如何在C#中使用StreamWriter写入文件?
我无法正确写入文件。该程序读取文件并在控制台上显示毫无问题,但是我有一些问题试图写入此文件。
我有这样的方法:
public static string Temp(string f)
{
int v = f.IndexOf(":");
string rez = f.Substring(0, v + 1);
string st = f.Substring(v + 1);
int[] tm = Numbers(st, ' ', '\t');
if (tm == null)
return rez + "mistake";
else
return String.Format("{0} {1} {2} {3}", rez, tm.Average(), tm.Min(), tm.Max());
}
文件内容如下:
mistake
mistake
我的一些代码:
static void Main(string[] args)
{
Show();
Console.ReadKey();
}
public static void Show()
{
string file = "data3.txt";
char[] s = { ' ', '\t', '\r' };
int[][] jaggedArray;
ReadFile(file, s, out jaggedArray);
StreamWriter sw = new StreamWriter("result3.txt");
Result(file, jaggedArray, sw);
}
public static void ReadFile(string f, char[] s, out int[][] jaggedArray)
{
using (StreamReader n = new StreamReader(f))
{
string[] l = n.ReadToEnd().Split('\n');
jaggedArray = new int[l.Length][];
for (int i = 0; i < l.Length; i++)
{
string t = l[i].Replace("\r", String.Empty);
Console.WriteLine(Temp(t));
}
}
}
static void Result(string f, int[][] x, TextWriter n)
{
foreach (int[] tm in x)
{
n.WriteLine(Temp(f));
}
n.Close();
}
问题是temp(String f)
有效地工作并显示在控制台中但不在文件中的数据。我会很感谢您的帮助
I can't write into a file correctly. The program reads the file and displays it on console with no problem, but I have some issues trying to write into this file.
I have this method:
public static string Temp(string f)
{
int v = f.IndexOf(":");
string rez = f.Substring(0, v + 1);
string st = f.Substring(v + 1);
int[] tm = Numbers(st, ' ', '\t');
if (tm == null)
return rez + "mistake";
else
return String.Format("{0} {1} {2} {3}", rez, tm.Average(), tm.Min(), tm.Max());
}
File contents is returned like this:
mistake
mistake
Some of my code:
static void Main(string[] args)
{
Show();
Console.ReadKey();
}
public static void Show()
{
string file = "data3.txt";
char[] s = { ' ', '\t', '\r' };
int[][] jaggedArray;
ReadFile(file, s, out jaggedArray);
StreamWriter sw = new StreamWriter("result3.txt");
Result(file, jaggedArray, sw);
}
public static void ReadFile(string f, char[] s, out int[][] jaggedArray)
{
using (StreamReader n = new StreamReader(f))
{
string[] l = n.ReadToEnd().Split('\n');
jaggedArray = new int[l.Length][];
for (int i = 0; i < l.Length; i++)
{
string t = l[i].Replace("\r", String.Empty);
Console.WriteLine(Temp(t));
}
}
}
static void Result(string f, int[][] x, TextWriter n)
{
foreach (int[] tm in x)
{
n.WriteLine(Temp(f));
}
n.Close();
}
The thing is that Temp(string f)
works and shows data in console but not in file. I would appreciate some help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在业务逻辑上有很多问题:
我无能为力 ,
我尝试帮助文件操作。
为什么不摆脱
stream
,streamReader
\streamWriter
并使用file> file
类来读写并写入文件?You have many issues with business logics:
I can't help you with the business logic (I don't know what the routine is supposed to do), but
I try to help with file manipulations.
Why don't get rid of
Streams
,StreamReader
\StreamWriter
and useFile
class to read and write the file?