简体   繁体   English

Streamwriter 不会覆盖文件,保持追加或仅写入第一行

[英]Streamwriter will not overwrite file, keeps appending or writes only the first line

Been looking through other people's answers and nothing seems to work.一直在查看其他人的答案,似乎没有任何效果。

Here is my code:这是我的代码:

    public void TaskOnClick() //getting multi-values
    {
        foreach (string inputJson in File.ReadLines("Assets/Text/multi-import.txt"))
        {
            string temperature = GetTemperatureByRegex(inputJson);
            Debug.Log(temperature);

            string filename = "Assets/Text/TEMP/multi-export.txt";
            {
                using (StreamWriter writeFile = new StreamWriter(filename, false))
                {
                    writeFile.AutoFlush = true;
                    Console.SetOut(writeFile);
                    writeFile.WriteLineAsync(temperature.ToString());
                }
            }
        }

    }

The idea is that my parsing script gets my data and then streamwriter writes the data to a txt file.这个想法是我的解析脚本获取我的数据,然后流编写器将数据写入一个 txt 文件。 Problem is that streamwriter keeps appending the txt file instead of overwriting the file.问题是 streamwriter 不断追加 txt 文件而不是覆盖文件。 Whenever I try to use filestream it overwrites the file, yes, but only the first line of the data gets written, no matter what I tried.每当我尝试使用文件流时,它都会覆盖文件,是的,但无论我尝试什么,都只会写入数据的第一行。

My username speaks for itself...我的用户名不言自明...

Why don't you just delete the file before opening the stream writer?为什么不在打开流编写器之前删除文件?

if(File.Exists(filename)){
             File.Delete(filename);
             }
   //here whatever you need to do next

What you do wrong is creating StreamWriter inside a loop.你做错的是在循环内创建StreamWriter If you provide an overwrite settings it will only write 1 line.如果您提供覆盖设置,它只会写入 1 行。

public void TaskOnClick() //getting multi-values
    {
       string filename = "Assets/Text/TEMP/multi-export.txt";
       using (StreamWriter writeFile = new StreamWriter(filename, false))
       {
        foreach (string inputJson in File.ReadLines("Assets/Text/multi-import.txt"))
        {
            string temperature = GetTemperatureByRegex(inputJson);
            Debug.Log(temperature);  
            writeFile.AutoFlush = true;
            Console.SetOut(writeFile);
            writeFile.WriteLine(temperature.ToString());            
        }
      }
    }

But there is a shorter way of doing this with the help of LINQ.但是在 LINQ 的帮助下,有一种更短的方法可以做到这一点。

public void TaskOnClick() //getting multi-values
    {
       string filename = "Assets/Text/TEMP/multi-export.txt";            
       var tempratures = File.ReadAllLines("Assets/Text/multi-import.txt")
                         .Select(GetTemperatureByRegex).ToArray();
       File.WriteAllLines(filename,tempratures); // it creates a new file or overwrites

     }    

I have to state that above method may be dangerous if input file is too large.我必须声明,如果输入文件太大,上述方法可能很危险。 Because it reads entire file into memory.因为它将整个文件读入内存。

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

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