使用VB.NET读取和写入txt文件,其间包含单词

时间:2010-06-26 01:37:29

标签: vb.net visual-studio frameworks

我想打开一个txt文件并从numbers写入1 to 100并在每个号码enter之间插入。

4 个答案:

答案 0 :(得分:2)

您可以尝试的一种方法是将数字写入StringBuilder,然后使用它的ToString()方法获取结果文本:

Imports System.IO
Imports System.Text


Public Class NumberWriter
   Private ReadOnly OutputPath as String = _
          Path.Combine(Application.StartupPath, "out.txt")


   Public Sub WriteOut()
       Dim outbuffer as New StringBuilder()

       For i as integer = 1 to 100
          outbuffer.AppendLine(System.Convert.ToString(i))
       Next i

       File.WriteAllText(OutputPath, outbuffer.ToString(), true)
   End Sub

   Public Shared Sub Main()
      Dim writer as New NumberWriter()
      Try
        writer.WriteOut()
      Catch ex as Exception
        Console.WriteLine(ex.Message)
      End Try
   End Sub
End Class

答案 1 :(得分:1)

在家和学习方面有一个很好的例子

 Dim FILE_NAME As String = "C:\test2.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
    objWriter.Write(TextBox1.Text)
    objWriter.Close()
    MsgBox("Text written to file")
Else
    MsgBox("File Does Not Exist")
End If

答案 2 :(得分:1)

您还可以使用“My.Computer.FileSystem”命名空间,例如:

Dim str As String = ""
For num As Int16 = 1 To 100
  str += num.ToString & vbCrLf
Next
My.Computer.FileSystem.WriteAllText("C:\Working\Output.txt", str, False)

答案 3 :(得分:0)

请参阅System.IO namespace,尤其是System.IO.File class