NelloRizzo

[VBNET] Streams

Feb 16th, 2017
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.88 KB | None | 0 0
  1. Imports System.IO
  2. Module Module1
  3.  
  4.     Sub Main()
  5.         Try
  6.             Dim s As System.IO.Stream = Nothing
  7.             ' stream per file
  8.             Dim f As New FileStream(".\test.txt", FileMode.Create)
  9.             ' stream che usa la memoria come endpoint
  10.             Dim m As New MemoryStream()
  11.             ' stream di rete
  12.             Dim n As Net.Sockets.NetworkStream = Nothing
  13.             ' su tutti gli stream si può operare con buffer
  14.             Dim bf As New BufferedStream(f)
  15.             ' Per leggere o scrivere oggetti si usano TextReader e TextWriter
  16.             Using r As TextReader = New StreamReader(m)
  17.                 ' tutti gli oggetti che implementano l'interfaccia di sistema
  18.                 ' IDisposable
  19.                 Using w As TextWriter = New StreamWriter(m)
  20.                     Dim line As String
  21.                     Do
  22.                         line = Console.ReadLine()
  23.                         'Dim buffer() As Byte =
  24.                         '    System.Text.Encoding.ASCII.GetBytes(line)
  25.                         'bf.Write(buffer, 0, buffer.Length)
  26.                         w.WriteLine(line)
  27.                     Loop Until line = "."
  28.                     'w.Close() ' la istruzione USING evita la necessità di CHIUDERE l'oggetto
  29.                     w.Flush()
  30.                     m.Seek(0, SeekOrigin.Begin)
  31.                     Dim c As String = String.Empty
  32.                     Do
  33.                         c = r.ReadLine()
  34.                         If Not String.IsNullOrEmpty(c) Then Console.WriteLine(c)
  35.                     Loop Until String.IsNullOrEmpty(c)
  36.                 End Using
  37.             End Using
  38.         Catch ex As IOException
  39.             Console.WriteLine("Problemi in lettura o scrittura su stream.")
  40.         Catch ex As Exception
  41.             Console.WriteLine("Altri errori")
  42.         End Try
  43.  
  44.         Console.ReadLine()
  45.     End Sub
  46.  
  47. End Module
Add Comment
Please, Sign In to add comment