VB .NET-文本文件读写

读取指定分隔符格式文本文件:

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\TestFolder\test.txt")
   MyReader.TextFieldType = FileIO.FieldType.Delimited
   MyReader.SetDelimiters(",")
   Dim currentRow As String()

   While Not MyReader.EndOfData
      Try
         currentRow = MyReader.ReadFields()
         Dim currentField As String
         For Each currentField In currentRow
            MsgBox(currentField)
         Next
      Catch ex As Microsoft.VisualBasic.
                  FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
      End Try
   End While

End Using

Using  根据需要获取该代码块控制的系统资源 

Using sw As StreamWriter = New StreamWriter(fileName, True, fileEncoding)
    If Not IsNothing(fileModel.AddInfo) Then                    
        For Each addInfo As String In fileModel.AddInfo                 
            sw.WriteLine(addInfo)                   
        Next                    
    End If                  


    If hasHeader Then                   
        sw.WriteLine(fileModel.Header)                  
    End If                  


    Me.CSVFileFromArray(fileModel.DataBody, sw, blnAddDoubleQuotes)                 


    If displayMsg Then                  
        Message.Show("I1001", "CSV")                    
    End If                  

    Return True                 
End Using

 

展开阅读全文

Delphi文本文件读写

12-03

Delphi文本文件读写 rnrnDelphi 支持三种文件类型: 文本文件、记录文件、无类型文件。rnrn文本文件是以行为单位进行读、写的。由于每一行的长度不一定相同,不能计算出给定行在文件中的确切位置,因而只能顺序地读写。rnrn文本文件只能单独为读或写而打开,在一个打开的文本文件上同时进行读、写操作是不允许的。rnrn文本文件的打开需要两个步骤:1.文件变量与文件名关联;2.初始化读写。rnrn1.文件变量与文件名关联:rnAssignFile(VarTxt, FileName);rnFileName 如果省略路径将默认当前目录。rnrn2.初始化读写有三种方式:rn(1) Reset: 只读打开, 指针移到文件头;rn(2) Rewrite: 创建新文件并打开, 只写;rn(3) Append: 从尾部追加, 指针当然在文件尾。rnrn文件不存在时使用 Reset 或 Append 会引发一个I/O异常。rnrn最后用 CloseFile 关闭文件。rnrn为保持兼容,Delphi 也允许用户用 Assign 建立关联, 用 Close 关闭文件。rnrn--------------------------------------------------------------------------------rnrnrn//读写示例: rnunit Unit1; rninterface rnuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; rntype TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); private Private declarations public Public declarations end; rnvar Form1: TForm1; rnimplementation rn$R *.dfm rnvar F: Text; //TextFile 和 Text 是一样的 FileName: string = 'c:\temp\test.txt'; rn//写文件procedure TForm1.Button1Click(Sender: TObject);begin AssignFile(F,FileName); Rewrite(F); //会覆盖已存在的文件 rn Writeln(F, '第一行'); Writeln(F, '第二行'); rn CloseFile(F);end; rn//读文件(先执行写文件)procedure TForm1.Button2Click(Sender: TObject);var s: string;begin AssignFile(F,FileName); Reset(F); //只读打开 rn Readln(F, s); //读取 ShowMessage(s); //显示: 第一行 Readln(F, s); //继续读取 ShowMessage(s); //显示: 第二行 rn CloseFile(F);end; rn//追加procedure TForm1.Button3Click(Sender: TObject);begin AssignFile(F,FileName); Append(F); //打开准备追加 rn Writeln(F, '第三行'); Writeln(F, '第四行'); rn CloseFile(F);end; rn//读取全部(需要Memo帮忙显示)procedure TForm1.Button4Click(Sender: TObject);var s: string;begin AssignFile(F,FileName); Reset(F); //只读打开 rn Memo1.Clear; while not Eof(F) do begin Readln(F, s); Memo1.Lines.Add(s); end; rn CloseFile(F);end; rn//分列写入procedure TForm1.Button5Click(Sender: TObject); //这个过程级函数的功能是: 用空格把 s 凑够 n 的长度 function AddSpace(s: string; n: Word): string; begin while Length(s) < n do begin s := s + ' '; end; Result := s; end;var name: string[8]; address: string[16];begin AssignFile(F, FileName); Rewrite(F); rn name := '张三'; name := AddSpace(name,8); address := '山东泰安'; address := AddSpace(address,16); Writeln(F, name, address); rn name := '李四儿'; name := AddSpace(name,8); address := '贵州省安顺黄果树'; address := AddSpace(address,16); Writeln(F, name, address); rn name := '王二麻子'; name := AddSpace(name,8); address := AddSpace(address,16); Writeln(F, name, address); rn CloseFile(F);写入的结果是: 张三 山东泰安 李四儿 贵州省安顺黄果树 王二麻子北京市海淀区end; rn//分列读取procedure TForm1.Button6Click(Sender: TObject);var name: string[8]; address: string[16];begin AssignFile(F, FileName); Reset(F); rn Memo1.Clear; while not Eof(F) do begin Readln(F, name); Memo1.Lines.Add(name); end; rn CloseFile(F); Reset(F); rn while not Eof(F) do begin Readln(F, name, address); Memo1.Lines.Add(address); end; rn CloseFile(F);读取结果: 张三 李四儿 王二麻子 山东泰安 贵州省安顺黄果树 北京市海淀区 end; rn//不同数据类型的写入和读取procedure TForm1.Button7Click(Sender: TObject);var name: string[6]; age: Word; birthday: TDate;begin AssignFile(F,FileName); Rewrite(F); rn name := '菜花 '; //加两个空格凑够6个字符 age := 18; birthday := StrToDate(DateToStr(Now-18*365)); //假如她今天过生日 Writeln(F,name,age,birthday); rn CloseFile(F); Reset(F); rn Readln(F,name,age,birthday); Memo1.Clear; Memo1.Lines.Add(name); Memo1.Lines.Add(IntToStr(age)); Memo1.Lines.Add(DateToStr(birthday)); rn CloseFile(F); rn读取结果: 菜花 18 1989-12-23//其实这样的东西应该用类型文件操作更合适, 但如果有这样的文本文件让你读取呢?end; rnend. rn 论坛

没有更多推荐了,返回首页