逐行读取文本文件 [英] read a text file line by line

查看:121
本文介绍了逐行读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要逐行从文本文件中读取数据.每行包含一个字符串或一个整数.我想使用StreamReader从文本文件逐行读取,并使用StreamWriter将其写入二进制文件.写入二进制文件"部分将很容易.需要逐行读取文本文件"部分是我需要帮助的部分.

I need to read data from a text file line by line. Each line contains either a string or an integer. I want to use StreamReader to read line by line from the text file and StreamWriter to write it to a binary file. The "write to binary file" part will be easy. The "read from text file line by line" part is the part I need help with.

推荐答案

在c#中,您可以执行以下操作.

In c# you can do something like this.

string loc = "idk/where/ever";
using(var sr = new StreamReader(loc))
  using(var sw = new StreamWriter(loc+".tmp"))
    {
     string line; 
        while((line=sr.ReadLine())!=null)
           {
              sw.WriteLine(line);
                //edit it however you want
           }
     }
File.Delete(loc);
File.Move(loc+".tmp",loc);

这篇关于逐行读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆