您现在的位置: 爱51代码网 >> 范文 >> 文章正文
vb.net2008读取txt文件代码

vb.net2008 如何读取txt文件,文件里是一行一行的,每一行是一个邮箱,有的时候行是空格, 去掉空格读取所有行, 再循环写到数据库中string[] Lines=File.ReadAllLines(@"C:\1.txt",Encoding.GetEncoding("gb2312"))
删除Lines中的空行,再写到数据库
你可以一行一行的读,当发现readline值为空时不要写入即可。
Dim fileContents As String
     Dim arrText As New ArrayList()

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                strFileName = OpenFileDialog1.FileName
                fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
            Catch ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

        Dim strReader As New StringReader(fileContents)
       Dim sLine As String = ""
       Dim i As Integer

       Do
           sLine = strReader.ReadLine()
           If Not sLine Is Nothing Then
                arrText.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        strReader.Close()
Imports System.IO
Imports System.Web

Public Class EmailList

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
        '是system.io方法读取
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(Application.StartupPath & "\files\user_email.Txt", System.Text.Encoding.Default)

        Dim line As String
        ListBox1.Items.Clear()
        line = "test" '随便赋一个初值
        '添加到控件中
        Do While sr.Peek() >= 0
            line = sr.ReadLine
            ListBox1.Items.Add(line)
        Loop
        Label1.Text = "总共读取" & ListBox1.Items.Count


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim newList As New ArrayList

        With ListBox1
            For i = 0 To .Items.Count - 1
                If Not newList.Contains(.Items(i)) Then
                    newList.Add(.Items(i))
                End If
            Next i
            .Items.Clear()
            For i = 0 To newList.Count - 1
                Dim StrA As String = newList(i)
                Dim StrB As String = "@"
                Dim Stu As Boolean = InStr(StrA, StrB)
                If Stu = True Then
                    .Items.Add(newList(i))
                Else

                End If


            Next i
        End With
        Label2.Text = " 整理后,总共" & ListBox1.Items.Count
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim SaveFileName As String
        Dim myStreamWriter As StreamWriter
        SaveFileName = Application.StartupPath + "\files\ok.txt"
        myStreamWriter = File.CreateText(SaveFileName)
        Dim MyOutput As String

        '循环写入TXT

        With ListBox1

            For i = 0 To ListBox1.Items.Count - 1
                MyOutput = ListBox1.Items(i)
                myStreamWriter.WriteLine(MyOutput)
            Next i
        End With

        myStreamWriter.Close()
        Label5.Text = "写入成功"
    End Sub
End Class

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    fastreport提示没有找到fr_class
    cximge的图如何存入数据库并提取
    linux下编译faac及faad2提示 rec
    TClientDataSet如何避免多人更新
    如何用delphi代码修改XML节点
    vc++ pvoid *用delphi怎么表达
    GIS用百度行政区域的经纬度直接画
    不小心卸载了.net 4 extend,vs2
    C#byte【】里的数转换成string时
    C#的session取不到值
    java如何判断一个字符串里的数字
    从未对字段“iosQueryServer
    .net线程的事件没有执行
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |