繁体   English   中英

c#中如何用StreamWriter写入文件?

[英]How to write into a file with StreamWriter in c#?

我无法正确写入文件。 该程序读取文件并将其显示在控制台上没有问题,但我在尝试写入该文件时遇到了一些问题。

我有这个方法:

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)可以工作并在控制台而不是文件中显示数据。 我会很感激一些帮助

您在业务逻辑方面有很多问题:

//TODO: Bad Practice: names are distracting: Result writes into file, it doesn't solve for result 
//TODO: Bad practice: names are unreadable: no-one can guess that n is writer (and not, say, a count)
static void Result(string f, int[][] x, TextWriter n)
{
    //TODO: unreadable name - what is "tm in x"?
    foreach (int[] tm in x)
    {
        //TODO: possibly an error: you ignore x and tm at all
        // instead you write a (processes) filename (f) into the file (n) // what names! tm, x, n... 
        n.WriteLine(Temp(f));
    }
    //TODO: bad practice - closing / disposing instance which is not created by the routine
    n.Close();
}

我无法在业务逻辑方面帮助您(我不知道例程应该做什么),但我会尽力帮助您处理文件操作。

为什么不摆脱StreamsStreamReader \ StreamWriter而使用File class 来读写文件?

using System.Linq;
...

// why do we need out here? Let's just return the result
static int[][] ReadFile(string fileName) {
  // Here we read all file's lines into string array - l
  string[] lines = File.ReadAllLines(fileName);
     
  // From now on we can forget about file and manipulate with 
  // lines - lines read from the file 
  result = new int[lines.Length][];

  // Note, that you put nothing into result, just print out lines
  foreach (string line in lines)
    Console.WriteLine(Temp(line));

  return result;
}

static void WriteFile(string fileName, int[][] data) {
  // Here we prepare lines to be written into the file
  // for each array and its index within jagged array x we should
  // create a line to be written.
  // Here we ignore both array and index and put the file name instead - Temp(f)
  // TODO: provide correct business logic here:
  //    (array, index) => what should be written into file's line
  var lines = data
    .Select((array, index) => Temp(fileName)); 

  // Then we just write the lines prepared into the file
  File.WriteAllLines(fileName, lines);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM