How to Write Visual Basic Array Data to a Text File

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Write Visual Basic Array Data to a Text File
(Jupiterimages/Pixland/Getty Images)

The Visual Basic programming language from Microsoft has a number of methods that can be used to write text files, but the simplest uses the "My.Computer.FileSystem.OpenTextFileWriter" method. To write an array to the text file, simply iterate through the array, writing one element on each line.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Create a new project in Visual Basic by clicking "File" and "New Project." Select a "Console Application" as your project type. Later, you can use the same code, without modification, to write Visual Basic arrays to a text file within the context of a Windows Form Application.

  2. 2

    Create a test array by pasting the following into your project:

    Dim array() as Integer = {32, 45, 65}

  3. 3

    Create the file and assign it to a variable:

    Dim f = My.Computer.FileSystem.OpenTextFileWriter("data.txt", False)

    Replace "data.txt" with the path and name of the file to which you want to write the array.

  4. 4

    Write all the data in the array to the file, one entry per line:

    For Each x In array f.WriteLine(x) next f.Flush() f.Close()

  5. 5

    Press the green arrow button to run the program.

Tips and warnings

  • This example used an array of Integers, but any data that can be converted to a string will write successfully to a text file using this method.
  • The following code will read the data back into an array:
  • Dim read = My.Computer.FileSystem.OpenTextFileReader("data.txt")
  • Dim arrList as LinkedList(Of Integer) = New LinkedList(Of Integer)
  • while (not read.EndOfStream)
  • arrlist.AddLast(Integer.Parse(read.readLine))
  • end while
  • dim arr2 = arrlist.ToArray()

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.