English 中文(简体)
Is there a mono (.Net) equivalent to the Scanner-class in Java
原标题:

In Java there is a nice class called Scanner for reading strings and streams of a certain format to data-structures like integers, floats,... Is there any equivalent in Mono? In .Net there is a binary-reader, but it reads the data as binaries. I want a class that read the data by parsing it from a string.

EDIT: reaction on the current answers, I know most of the data-types have a parse-method, but the problem with these are, they don t read the string sequentially. In Java you simply initialize the object, and by activating methods, you sequentially read the data-types.

最佳回答

Well, since you are reading a file in for test cases, I m going to assume you know the exact format of the data coming in and how it needs to be parsed. Looking at Java s Parser class in the documentation does make it look very complicated or much more than a convenience class. It appears to just tokenize the stream string on spaces and provide functions for taking the token and parsing it. And as far as I know, .Net doesn t have an equivalent class. So if you want one for reuse, then you ll need to write one yourself.

But the basic algorithm you need is to read the file contents, split the tokens, and parse them. If your file is reasonable sized, you could use File.ReadAllLines() which reads the contents of the file line-by-line into a String[]. Or you could use a StreamReader class to read the lines one at a time using its ReadLine(). Then once you have them in a line, you would tokenize them with String s Split(), and then parse them.

Not knowing exactly how you need to use the values in the file, a very basic pattern could be:

// Read in the whole file
String[] strLines = File.ReadAllLines(strFilePath);

// Loop through each of the lines to process their contents
foreach (String strLine in strLines) {
    // Tokenize on the spaces
    String[] strTokens = strLine.Split(   );

    // Parse the values
    int iIntValue = Int.Parse(strTokens[0]);
    double dDoubleValue = Double.Parse(strTokens[1]);

    // Do something with the values
}

Creating your own Parser class would allow you to maintain the state and auto advance tokens as you use them. If you go that route, the StreamReader may be the best option.

问题回答

Most basic data types will have a .Parse and .TryParse() methods that will do that for you.

For example the Int32.TryParse method.

In .NET, many of the type have static .Parse() and .TryParse() methods that takes a String parameter.

You could do something like:

Stream s = new Stream();
// Wire up the stream.

StreamReader reader = new StreamReader(s);
String value = reader.ReadToEnd();

int valueAsInt = Int32.TryParse(value);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签