阅读文本文件 [英] Reading a Text File

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

问题描述

我正在尝试使用

删除文本文件中的多行


Private Sub Read_TextFile()

Dim objReader As StreamReader

Dim strfull,strContents,strContentsold,strContentsnew As

String

objReader = New StreamReader(" C:\answer .txt")

''清除文本框1

TextBox1.Clear()

strContentsold =""

strContentsnew =""

strContents =""

strfull =""

Do While Not objReader。 EndOfStream

strContentsold = strContentsnew

strContentsnew = objReader.ReadLine


如果strContentsnew = strContentsold那么

strContents =""

Else

strContents = strContentsnew

结束如果


strfull + = strContents

循环


TextBox1.Text = strfull


objReader.Close()

结束子


文本将存储在TextBox1中。


但是,它看起来不起作用!我想知道是否有人有任何想法。这是棕褐色原始文本文件作为示例


我假设您想知道我是否可以告诉您威尔士。

我认为您想要知道我是否可以告诉你威尔士。

我是否可以告诉你关于威尔士

威尔士是英国的行政部门。

Wales

行政区划

英国

资料来源:START KB

资料来源:

返回START对话框窗口。

返回START对话框窗口。

返回START对话框窗口。

I am trying to delete multiple lines in a text file using the
following

Private Sub Read_TextFile()
Dim objReader As StreamReader
Dim strfull, strContents, strContentsold, strContentsnew As
String
objReader = New StreamReader("C:\answer.txt")
''Clear the Text Box1
TextBox1.Clear()
strContentsold = ""
strContentsnew = ""
strContents = ""
strfull = ""
Do While Not objReader.EndOfStream
strContentsold = strContentsnew
strContentsnew = objReader.ReadLine

If strContentsnew = strContentsold Then
strContents = ""
Else
strContents = strContentsnew
End If

strfull += strContents
Loop

TextBox1.Text = strfull

objReader.Close()
End Sub

The text will be stored in TextBox1.

However, it appears not to work! I was wondering if anybody had any
ideas. Here is tan original text file as an example

I assume that you wanted to know whether I can tell you about Wales.
I assume that you wanted to know whether I can tell you about Wales.
whether I can tell you about Wales
Wales is an administrative division in the UK.
Wales
an administrative division
the UK
Source: START KB
Source:
Go back to the START dialog window.
Go back to the START dialog window.
Go back to the START dialog window.

推荐答案

然而,它似乎无法正常工作!


你得到了什么输出?
However, it appears not to work!

What output did you get?


8月11日下午3:22,BobRoyAce< b ... @ omegasoftwareinc.comwrote:
On Aug 11, 3:22 pm, BobRoyAce <b...@omegasoftwareinc.comwrote:

但是,它似乎不起作用!
However, it appears not to work!



你得到了什么输出?


What output did you get?



与输入相同!

Same as the input!


您好,


它对我来说确实很好,但是,代码有点老式时尚

(vb6)风格。请注意,Net中的所有内容都是一个对象,同时告诉

不再使用某些字符串了。


如果附加字符串,则为stringbuilder更合适,因为

每次创建一个更长的新字符串时附加到字符串。 (be b / b
也知道使用+作为字符串连接器在某些情况下可能会给出b $ b想要的结果,使用真正的连接字符& for that that that tell

更直接的是它是关于字符串然后用str来为所有内容添加前缀。


我改变了你的代码。


\\\

Private Sub Read_TextFile()

Dim Reader As IO.StreamReader

Dim ContentsOld,ContentsNew As String

Dim Contents As New System.Text.StringBuilder

Reader = New IO.StreamReader(" C:\\\\answer.txt")

TextBox1.Clear()

ContentsOld =""

ContentsNew =""

Do While Not Reader。 EndOfStream

ContentsOld = ContentsNew

ContentsNew = Reader.ReadLine

如果ContentsNew< ContentsOld那么

ContentsOld = ContentsNew

Contents.Append(ContentsNew)

结束如果

L oop

TextBox1.Text = Contents.ToString

Reader.Close()

结束子

///


Cor


< kr ******* @ yahoo.co.ukschreef in bericht

新闻:58 ********************************** @ w39g2000 prb.googlegroups.com ...
Hi,

It did work fine for me, however, the code is a little bit very old fashion
(vb6) style. Be aware that everything in Net is an object, while telling
that something is a string is not done anymore.

In the case of appending strings is the stringbuilder more suitable because
that as you append to a string everytime a longer new string is created. (Be
also aware that using the + as a string connecter can give in some cases not
wanted results, use the real connection character & for that, that tells
more direct that it is about string then to prefix everything with str.

I changed your code a little bit.

\\\
Private Sub Read_TextFile()
Dim Reader As IO.StreamReader
Dim ContentsOld, ContentsNew As String
Dim Contents As New System.Text.StringBuilder
Reader = New IO.StreamReader("C:\test\answer.txt")
TextBox1.Clear()
ContentsOld = ""
ContentsNew = ""
Do While Not Reader.EndOfStream
ContentsOld = ContentsNew
ContentsNew = Reader.ReadLine
If ContentsNew <ContentsOld Then
ContentsOld = ContentsNew
Contents.Append(ContentsNew)
End If
Loop
TextBox1.Text = Contents.ToString
Reader.Close()
End Sub
///

Cor

<kr*******@yahoo.co.ukschreef in bericht
news:58**********************************@w39g2000 prb.googlegroups.com...

>我正在尝试使用

删除文本文件中的多行


Private Sub Read_TextFile()

Dim objReader As StreamReader

Dim strfull,strContents,strContentsold,strContentsnew As

String

objReader = New StreamReader(" C:\answer.txt")

''清除文本框1

TextBox1.Clear()


strContentsold =""

strContentsnew =""

strContents =""

strfull = "


Do While Not objReader.En dOfStream


strContentsold = strContentsnew

strContentsnew = objReader.ReadLine


如果strContentsnew = strContentsold那么

strContents =""

Else

strContents = strContentsnew

结束如果


strfull + = strContents


循环


TextBox1.Text = strfull


objReader。关闭()

结束子


文本将存储在TextBox1中。


然而,它似乎没有上班!我想知道是否有人有任何想法。这是棕褐色原始文本文件作为示例


我假设您想知道我是否可以告诉您威尔士。

我认为您想要知道我是否可以告诉你威尔士。

我是否可以告诉你关于威尔士

威尔士是英国的行政部门。

Wales

行政区划

英国

资料来源:START KB

资料来源:

返回START对话框窗口。

返回START对话框窗口。

返回START对话框窗口。
>I am trying to delete multiple lines in a text file using the
following

Private Sub Read_TextFile()
Dim objReader As StreamReader
Dim strfull, strContents, strContentsold, strContentsnew As
String
objReader = New StreamReader("C:\answer.txt")
''Clear the Text Box1
TextBox1.Clear()
strContentsold = ""
strContentsnew = ""
strContents = ""
strfull = ""
Do While Not objReader.EndOfStream
strContentsold = strContentsnew
strContentsnew = objReader.ReadLine

If strContentsnew = strContentsold Then
strContents = ""
Else
strContents = strContentsnew
End If

strfull += strContents
Loop

TextBox1.Text = strfull

objReader.Close()
End Sub

The text will be stored in TextBox1.

However, it appears not to work! I was wondering if anybody had any
ideas. Here is tan original text file as an example

I assume that you wanted to know whether I can tell you about Wales.
I assume that you wanted to know whether I can tell you about Wales.
whether I can tell you about Wales
Wales is an administrative division in the UK.
Wales
an administrative division
the UK
Source: START KB
Source:
Go back to the START dialog window.
Go back to the START dialog window.
Go back to the START dialog window.


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

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