C# 中的 FileReader 类
我正在寻找快速的类来处理文本文件并舒适地阅读不同的对象(像 NextInt32、NextDouble、NextLine 等方法)。你能给我一些建议吗?
编辑: BinaryReader 在我的例子中是一个糟糕的类。我的数据格式不是二进制的。我有这样的文件
1 2 3
FirstToken NextToken
1.23 2,34
我想用如下代码读取这个文件:
int a = FileReader.NextInt32();
int b = FileReader.NextInt32();
int c = FileReader.NextInt32();
int d = FileReader.NextString();
int e = FileReader.NextString();
int f = FileReader.NextDouble();
int g = FileReader.NextDouble();
Edit2: 我正在寻找 Java 的模拟扫描仪
I am looking for fast class for to work with text files and comfortable reading different object (methods like NextInt32, NextDouble, NextLine, etc). Can you advice me something?
Edit: BinaryReader is bad class in my case. Format of my data is not binary. I have file like
1 2 3
FirstToken NextToken
1.23 2,34
And I want read this file with code like:
int a = FileReader.NextInt32();
int b = FileReader.NextInt32();
int c = FileReader.NextInt32();
int d = FileReader.NextString();
int e = FileReader.NextString();
int f = FileReader.NextDouble();
int g = FileReader.NextDouble();
Edit2: I am looking for analog Scanner from Java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我相信
TextReader
的这个扩展方法可以解决问题:它可以这样使用:
[编辑] 根据问题评论中的要求,这里是上述内容的实例包装版本使用分隔符和
CultureInfo
进行参数化:示例用法:
I believe this extension method for
TextReader
would do the trick:It can be used thus:
[EDIT] As requested in question comments, here's an instance wrapper version of the above that is parametrized with delimiters and
CultureInfo
:Sample usage:
我将把它添加为一个单独的答案,因为它与我已经给出的答案完全不同。以下是您开始创建自己的 Scanner 类的方法:
I'm going to add this as a separate answer because it's quite distinct from the answer I already gave. Here's how you could start creating your own Scanner class:
您应该准确定义您的文件格式。你会如何表示一个带有空格的字符串?是什么决定了行终止符的走向?
一般来说,您可以使用
TextReader
及其ReadLine
方法,然后使用double.TryParse
、int.TryParse
等 -但您需要首先确定格式。You should define exactly what your file format is meant to look like. How would you represent a string with a space in it? What determines where the line terminators go?
In general you can use
TextReader
and itsReadLine
method, followed bydouble.TryParse
,int.TryParse
etc - but you'll need to pin the format down more first.您检查过 BinaryReader 类吗?是的,它是一个文本文件,但没有什么可以阻止您将其视为二进制数据,因此可以使用 BinaryReader。它具有您正在寻找的所有方法(ReadLine 除外)。然而,在 BinaryReader 之上实现该方法并不会太困难。
Have you checked out the BinaryReader class? Yes it's a text file but there is nothing stopping you from treating it as binary data and hence using BinaryReader. It has all of the methods that you are looking for with the exception of ReadLine. However it wouldn't be too difficult to implement that method on top of BinaryReader.
如果您确实需要文本文件(即UTF-8或ASCII编码),那么二进制编写器将无法工作。
您可以使用
TextReader
,但与BinaryReader
和TextWriter
不同,它不支持除 Line 之外的任何类型和字符
。您必须定义允许使用哪些分隔符并自行解析线路基础数据。If you do need text files (ie UTF-8 or ASCII encoding) then the binary writer will not work.
You can use the
TextReader
, but unlike theBinaryReader
and theTextWriter
it does not support any types other than Line andchar
. You will have to define what separators are allowed and parse the Line base data yourself.System.IO.BinaryReader 类就是您所需要的。
ReadLine 方法的实现示例:
使用此类的示例:
The System.IO.BinaryReader class is what you need.
Example of implementation of a ReadLine method:
Example for using this class:
您可以使用 System.IO.File读取文件的类和 System.Convert< /a> 解析从文件中读取的字符串。
其中
TYPE
是您在该特定行/文件中处理的任何类型。如果一行上有多个值,您可以进行拆分并读取各个值,例如
You can probably use the System.IO.File Class to read the file and System.Convert to parse the strings you read from the file.
Where
TYPE
is whatever type you're dealing with at that particular line / file.If there are multiple values on one line you could do a split and read the individual values e.g.