Page 1 of 1

Saving & Loading 1d Array, 2d Array & Lists Rate Topic: -----

#1 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Post icon  Posted 26 March 2009 - 02:07 PM

Saving an 1D Array
 Private Sub SaveTheArray(ByRef TheArray() As String, ByRef Loci As String)
  If My.Computer.FileSystem.FileExists(Loci) Then
   Dim reply As System.Windows.Forms.DialogResult = MessageBox.Show("Overwrite file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
   If reply = Windows.Forms.DialogResult.No Then
	MessageBox.Show("Save Stopped", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
	Exit Sub
   End If
   My.Computer.FileSystem.DeleteFile(Loci)
  End If
  Try
   Dim fs As New IO.StreamWriter(Loci, IO.FileMode.Truncate)
   For Each ae As String In TheArray
	fs.WriteLine(ae)
   Next
   fs.Close()
  Catch ex As System.IO.IOException
   Throw ex
  End Try
 End Sub



Loading a 1-D Array
 Private Sub LoadTheArray(ByRef TheArray() As String, ByRef loci As String)
  If My.Computer.FileSystem.FileExists(loci) = False Then
   MessageBox.Show("File not found")
   Exit Sub
  End If
  Try
   Dim fs As New IO.StreamReader(loci)
   Dim i As Integer = 0
   While fs.EndOfStream = False
	ReDim Preserve TheArray(i)
	TheArray(i) = fs.ReadLine
	i += 1
   End While
  Catch ex As System.IO.IOException
   Throw ex
  End Try
 End Sub




Saving the 2D Array out of a file.
 Private Sub Save_2D_Array(ByRef b(,) As Integer, ByRef loci As String)
  ' Does the file already exist?
  If My.Computer.FileSystem.FileExists(loci) Then
   ' Yes, the file exists.
   ' Ask the user to decide wether to continue.
   Dim reply As System.Windows.Forms.DialogResult = MessageBox.Show("Overwrite file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
   If reply = Windows.Forms.DialogResult.No Then
	' The user doesn't want to contine. Display message, then exit.
	MessageBox.Show("Save Stopped", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
	Exit Sub
   End If
   ' Delete the existing file, other it appends (Why?)
   My.Computer.FileSystem.DeleteFile(loci)
  End If
  ' Using a Try - Catch Statement, just incase their is a problem with the file.
  Try
   ' Create a Stream into which we can write.
   Dim fs As New IO.StreamWriter(loci, IO.FileMode.Create)
   ' Temporary String to which we concatenate columns of each row.
   Dim line As String = String.Empty
   ' For-Loop each row
   For g_row As Integer = LBound(b, 1) To UBound(b, 1)
	' Remembering to Empty The String
	line = String.Empty
	' For-Loop each column
	For g_col As Integer = LBound(b, 2) To UBound(b, 2)
	 ' Is this the before the last column?
	 If g_col < UBound(b, 2) Then
	  ' Yes, so concatenate the contents at this row and column on the end of the temporary string.
	  ' Add a comma as well
	  line &= g(g_row, g_col) & ","
	 Else
	  ' Nope, Last Column so concatenate the contents without a comma.
	  line &= g(g_row, g_col)
	 End If
	Next
	' Write the string into the stream.
	' ** INFO ** The string isn't necassarly written into the file straight away.
	fs.WriteLine(line)
   Next
   ' Close the stream, at which point the stream is written into the file.
   fs.Close()
   ' Just in case the stream throws an exception catch.
  Catch ex As System.IO.IOException
   Throw ex
  End Try
 End Sub



Loading the 2D Array out of a file.
 Private Sub Load_2D_Array(ByRef b(,) As Integer, ByRef loci As String)
  Try
   Dim fs As New IO.StreamReader(loci)
   Dim line As String = ""
   Dim g_row As Integer = 0
   While fs.EndOfStream = False
	line = fs.ReadLine
	Dim c() As String = line.Split(",")
	Dim i As Integer = 0
	For Each ce As String In c
	 b(g_row, i) = CInt(ce)
	 i += 1
	Next
	g_row += 1
   End While
   fs.Close()
  Catch ex As System.IO.IOException
   Throw ex
  End Try
 End Sub




Saving a List
 Private Sub SaveTheList(ByRef TheList As List(Of String), ByRef Loci As String)
  If My.Computer.FileSystem.FileExists(Loci) Then
   Dim reply As System.Windows.Forms.DialogResult = MessageBox.Show("Overwrite file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
   If reply = Windows.Forms.DialogResult.No Then
	MessageBox.Show("Save Stopped", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
	Exit Sub
   End If
  End If
  Try
   Dim fs As New IO.StreamWriter(Loci, IO.FileMode.OpenOrCreate)
   For Each ae As String In TheList
	fs.WriteLine(ae)
   Next
   fs.Close()
  Catch ex As System.IO.IOException
   Throw ex
  End Try
 End Sub



Loading a List
 Private Sub LoadTheList(ByRef TheList As List(Of String), ByRef loci As String)
  If My.Computer.FileSystem.FileExists(loci) = False Then
   MessageBox.Show("File not found")
   Exit Sub
  End If
  TheList.Clear()
  Try
   Dim fs As New IO.StreamReader(loci)
   While fs.EndOfStream = False
	TheList.Add(fs.ReadLine)
   End While
  Catch ex As System.IO.IOException
   Throw ex
  End Try
 End Sub



Notes to remember
  • Writing to a stream doesn't mean it written straight into the file.
  • Load contents in the same order in which they were saved.
  • Treat the read as if it was is a string, cast to the correct type.


I personally prefer the method used the linked tutorial, as it save the missing about of casting string to different types.
Saving Objects Tutorial

Is This A Good Question/Topic? 0
  • +

Page 1 of 1