从文件中读取信息 [英] Reading Information from a File

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

问题描述

我在下面有这个代码,它将打开一个创建的文件,并将文件读取到最后的
。但是,如果我的文件的值为200,300,400由

逗号分隔,我如何读取每个单独的值并将其放入

arraylist?我需要做一些逗号分割,只读取

值而不是逗号。


Dim oFile As File

Dim oRead As System.IO.StreamReader

Dim EntireFile as String


尝试

oRead = oFile.OpenText(" C:\sample.txt")

EntireFile = oRead.ReadToEnd()

oRead.Close()


Catch ex Exception

''错误处理代码。

结束尝试

解决方案

< BLOCKQUOTE>" cmdolcet69" < co ************ @ hotmail.comschrieb


我在下面的代码中打开一个已创建的文件并阅读

文件到最后。但是,如果我的文件的值为200,300,400,则用逗号分隔

我如何读取每个单独的值并将其放入

arraylist?我需要做一些逗号分割,只读取

值而不是逗号。


Dim oFile As File

Dim oRead As System.IO.StreamReader

Dim EntireFile as String


尝试

oRead = oFile.OpenText(" C:\sample.txt")

EntireFile = oRead.ReadToEnd()

oRead.Close()


Catch ex As Exception

''错误处理代码。

结束尝试



看看方法String对象。它还提供了Split

功能。您可以使用它的

AddRange方法一次向arraylist添加几个项目。

Armin



使用MyReader作为新的Microsoft.VisualBasic.FileIO.TextFieldParser _

(" c:\logs\bigfile")

MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited

MyReader.Delimiters = New String(){vbTab}

Dim currentRow As String()

''遍历文件中的所有字段。

''如果任何行损坏,报告错误并继续解析。

虽然不是MyReader.EndOfData

尝试

currentRow = MyReader.ReadFields()

''在这里包含代码来处理行。

Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineExceptio n

MsgBox(" Line"& Ex.Message& _

"无效。跳过)

结束尝试

结束时

结束使用


On Sun,2007年9月30日15:58:29 -0700,cmdolcet69< co ************ @ hotmail.com>

写道:


>我在下面有这个代码,它将打开一个创建的文件并将文件读到最后。但是,如果我的文件的值为200,300,400,则用逗号分隔,我如何读取每个单独的值并将其放入
arraylist?我需要做一些逗号分割,只读取
值,而不是逗号。

Dim oFile as File

Dim oRead As System.IO。 StreamReader

Dim EntireFile As String


尝试

oRead = oFile.OpenText(" C:\sample.txt" )

EntireFile = oRead.ReadToEnd()

oRead.Close()


Catch ex As Exception

''错误处理代码。

结束尝试


Armin,


我不认为CMDolcet会回答这些问题。

http://groups.google .com / group / micro ... rch + this + group

Cor


I have this code below that will open a created file and read the file
to the end. However if my file has values 200,300,400 seperated by
commas how can i read in each seperate value and place it into an
arraylist? I need to do some sort of commas split to just read the
values and not the commas.

Dim oFile As File
Dim oRead As System.IO.StreamReader
Dim EntireFile As String

Try
oRead = oFile.OpenText("C:\sample.txt")
EntireFile = oRead.ReadToEnd()
oRead.Close()

Catch ex As Exception
''Error Handling Code.
End Try

解决方案

"cmdolcet69" <co************@hotmail.comschrieb

I have this code below that will open a created file and read the
file to the end. However if my file has values 200,300,400 seperated
by commas how can i read in each seperate value and place it into an
arraylist? I need to do some sort of commas split to just read the
values and not the commas.

Dim oFile As File
Dim oRead As System.IO.StreamReader
Dim EntireFile As String

Try
oRead = oFile.OpenText("C:\sample.txt")
EntireFile = oRead.ReadToEnd()
oRead.Close()

Catch ex As Exception
''Error Handling Code.
End Try


Have a look at the methods of the String object. It also provides a Split
function. You can add several items to an arraylist at once using it''s
AddRange method.
Armin



Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\logs\bigfile")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {vbTab}
Dim currentRow As String()
''Loop through all of the fields in the file.
''If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
'' Include code here to handle the row.
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineExceptio n
MsgBox("Line " & ex.Message & _
" is invalid. Skipping")
End Try
End While
End Using

On Sun, 30 Sep 2007 15:58:29 -0700, cmdolcet69 <co************@hotmail.com>
wrote:

>I have this code below that will open a created file and read the file
to the end. However if my file has values 200,300,400 seperated by
commas how can i read in each seperate value and place it into an
arraylist? I need to do some sort of commas split to just read the
values and not the commas.

Dim oFile As File
Dim oRead As System.IO.StreamReader
Dim EntireFile As String

Try
oRead = oFile.OpenText("C:\sample.txt")
EntireFile = oRead.ReadToEnd()
oRead.Close()

Catch ex As Exception
''Error Handling Code.
End Try


Armin,

I have not the idea that CMDolcet looks ever to the answers.

http://groups.google.com/group/micro...rch+this+group

Cor


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

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