如何打開,修改,并保存文本文件中的內容,急啊

解决方案 »

  1.   

    打开:
           CommonDialog1.Action = 1
            Open CommonDialog1.FileName For Input As #1
                text(Index).text = ""
                Do While Not EOF(1)
                     Line Input #1, inputdata
                     text1.text = text1.text + inputdata + Chr(13) + Chr(10)
                Loop
            Close #1
    保存:
        CommonDialog1.Action = 2
            Open CommonDialog1.FileName For Output As #1
                 Print #1, text(Index).text
          Close #1
      

  2.   

    '
    '读TEXT文件
    '函数:RedTextFile
    '参数:FileName 打开的TXT文件名.
    '返回值:成功 返回文件内容.失败  返回""
    Public Function RedTextFile(Filename As String) As String
         Dim FileID As Long
         Dim InputStr As String
         Dim LineStr As String
         
         On Error Resume Next
         
         InputStr = "": LineStr = ""
         FileID = FreeFile()
         Open Filename For Input As #FileID
              Do While Not EOF(FileID)           ' 循环至文件尾。
                 LineStr = ""
                 Line Input #FileID, LineStr
                 InputStr = InputStr & LineStr
              Loop
         Close #FileID
         RedTextFile = IIf(Err.Number = 0, InputStr, "")
         Err.Clear
    End Function'
    '写TEXT文件
    '函数:WritTextFile
    '参数:FileName 目标文件名.WritStr 写到目标的字符串.
    '返回值:成功 返回文件内容.失败  返回""
    '注:如果同名,目标字符串将覆盖原文件内容.
    Public Function WritTextFile(Filename As String, WritStr As String) As Boolean
    '/保存文件
        Dim FileID As Long, ConTents As String
        Dim A As Long, B As Long
        
        On Error Resume Next
        
        FileID = FreeFile
        Open Filename For Output As #FileID
             Print #FileID, WritStr
        Close #FileID
        WritTextFile = (Err.Number = 0)
        Err.Clear
    End Function
      

  3.   

    打开文件
    Open App.path & "\1.dat" For Output As #1
    将a的内容写文件
    Write #1,a
    再打开    
    Open App.path & "\1.dat" For Input As #1
    读文件的内容写入b
    Input #1, b
      

  4.   

    用FileSystemObject对象吧,
    有VB的“工程”菜单里选择“引用...”Microsoft Scripting RuntimePrivate Sub Form_Load()
           Dim Fobj As Scripting.FileSystemObject
           Dim Fs As TextStream
           Dim strText As String
           
           Set Fobj = New Scripting.FileSystemObject
           
           Set Fs = Fobj.OpenTextFile("路径+文件名", ForWriting)
           strText = "Write!Write!Write!Write!Write!Write!Write!"
           Fs.Write strText
           Fs.Close
           Set Fs = Nothing
           Set Fobj = Nothing
    End Sub
      

  5.   

    http://www.531d.com社区开张,欢迎大家光临,感谢大家支持!社区刚刚建立,现在需要招聘"管理员"和"版主"  ,希望大家经常来罐水!http://www.531d.com