【问题标题】:I can't read line-by-line from text file. How can I do?我无法从文本文件中逐行读取。我能怎么做?
【发布时间】:2015-07-30 12:48:40
【问题描述】:

在我从assets文件夹中得到一个文件(例如:a.txt)之后,我正在逐行读取这个文件

此代码部分 (line=reader.Readline()) 必须读取文件行的​​内容;但line 正在获取文件路径。

我想将文件中的所有行添加到列表中。(lines)

******这个项目是关于Universal App和**Windows Phone 8.1部分

    List<string> lines = new List<string>();

    public async void LoadFile(string file)
    {
        var InstallationFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
        var _file = await InstallationFolder.GetFileAsync(file);
        byte[] byteArray = Encoding.UTF8.GetBytes(file);
        MemoryStream stream = new MemoryStream(byteArray);
        using (var reader = new StreamReader(stream))
        {
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }
        }  
    }

【问题讨论】:

  • 有什么理由不使用File.ReadAllLines?
  • 您不是在读取文件,而是在读取作为参数接收的字符串。您正在将file 字符串转换为字节数组,然后您正在读取此字节数组
  • 你的项目类型是什么?
  • 我在网上看到这样的例子,我如何读取文件
  • 最好将参数命名为fileName 并将局部变量_file 更改为fileContents。那么错误就会非常清楚。

标签: c# list file line streamreader


【解决方案1】:

你可以这样做:

List<string> lines = new List<string>();

public void LoadFile(string file)
{
    lines = File.ReadAllLines(file).ToList();
}

但是,File.ReadAllLines(file) 是您要实现的...它返回一个数组。

只需在您的代码中使用它:

string[] lines = File.ReadAllLines(path);

【讨论】:

  • 我试过了,但我的 System.IO 不包含文件,所以我不这样做
  • @GökçeDemir 所以你可以在这里找到答案stackoverflow.com/questions/23924424/…
  • 我想看看这个页面@randolph var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; var file = await folder.GetFileAsync("sample.txt"); var contents = await Windows.Storage.FileIO.ReadTextAsync(file); 并且我写了 **content.contain(word) ** 但项目没有调试
【解决方案2】:

您没有使用StreamReader 阅读该文件。试试:

public async void LoadFile(string file)
    {          
        using (var reader = new StreamReader(file))
        {
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }
        }  
    }

file 是您的文件路径和名称。

【讨论】:

  • var InstallationFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); var _file = await InstallationFolder.GetFileAsync(file);
  • async without await? 还想知道您为什么要异步加载文件-您会在这里使用任何东西来证明吗? Using Async for File Access
  • 我看了这个页面,但我不使用 FileStream 我不知道为什么我的 System.IO 没有 File 和 FİleStream
【解决方案3】:
var InstallationFolder = await      Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var _file = await InstallationFolder.GetFileAsync("a.txt");
var content = await Windows.Storage.FileIO.ReadTextAsync(_file);
                        if (content.Contains(word))
                        {
                            lines.Add(word);   
                        }

我已根据您的建议编写了下面的代码。但是我收到一条错误消息,例如“mscorlib.ni.dll 中发生了'System.ArgumentOutOfRangeException' 类型的异常,但未在用户代码中处理 WinRT 信息:目标多字节代码页中不存在 Unicode 字符的映射。 附加信息:目标多字节代码页中不存在 Unicode 字符的映射。 目标多字节代码页中不存在 Unicode 字符的映射。 如果有这个异常的处理程序,程序可以安全地继续。"

此时我应该怎么做才能克服这个问题?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多