最佳解答者
How to read each line in a file

問題
解答
-
If in this case, after you can:
1. Use StreamReader (or other reader objects) to open the file
2. Use the ReadLine method in the Reader object to read line by line
3. Use the TryParse shared method in the Integer class to determine whether it is an integer and get the value if it is
4. If the TryParse method return False, then use the TryParse shared method in the Char class to determine whether it is an integer and get the value if it is. Then check whether the returned value is between "a" and "z" or "A" and "Z".
5. Close the Reader object.
Note that the above is one possible solution only and is not optimized. You may need to handle any exception raised in the code.
2008年11月1日 17:13 -
How about reading each line and put it in a string collection first? After that, you can get random access to the collection, e.g. if you would like to get the fifth line, then you can retrieve the line content by stringCollection(4). Once you got the line content, you can apply your checking routine for integer and character.
2008年11月1日 17:25
所有回覆
-
If in this case, after you can:
1. Use StreamReader (or other reader objects) to open the file
2. Use the ReadLine method in the Reader object to read line by line
3. Use the TryParse shared method in the Integer class to determine whether it is an integer and get the value if it is
4. If the TryParse method return False, then use the TryParse shared method in the Char class to determine whether it is an integer and get the value if it is. Then check whether the returned value is between "a" and "z" or "A" and "Z".
5. Close the Reader object.
Note that the above is one possible solution only and is not optimized. You may need to handle any exception raised in the code.
2008年11月1日 17:13 -
I want to get the fifth line or second line or fourth line of the data in the txt file, my code is like this:
Dim objectReader As New System.IO.StreamReader("C:\number.txt")
Do While objectReader.Peek() <> -1
output = Integer.Parse(objectReader.ReadLine())
Console.WriteLine(output)My txt file contains:
3
5
9
10
4
I can only show all the context of the file but not the fifth line I want to display, how to get the fifth line or second line or fourth line???
2008年11月1日 17:19 -
How about reading each line and put it in a string collection first? After that, you can get random access to the collection, e.g. if you would like to get the fifth line, then you can retrieve the line content by stringCollection(4). Once you got the line content, you can apply your checking routine for integer and character.
2008年11月1日 17:25