VB.Net Tutorial/Stream File/FileStream — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Содержание
- 1 Convert String to ASCII byte code array and save
- 2 Convert String to Unicode byte code array and save
- 3 FileStream.Length
- 4 Read a file by using SeekOrigin
- 5 Read byte array and convert it to ASCII string
- 6 Read byte array and convert it to Unicode string
- 7 Read byte using FileStream
- 8 Read into a buffer
- 9 Save UTF8Encoding string to text file
- 10 Use FileStream to write byte array
- 11 Write Bytes using FileStream
Convert String to ASCII byte code array and save
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim strSource As String
Dim bteEndode() As Byte
Dim myFileStream As New FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
strSource = "asdf"
bteEndode = Encoding.ASCII.GetBytes(strSource)
myFileStream.Write(bteEndode, 0, bteEndode.Length)
myFileStream.Close()
End Sub
End Class
Convert String to Unicode byte code array and save
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim strSource As String
Dim bteEndode() As Byte
Dim myFileStream As New FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
strSource = "asdf"
bteEndode = Encoding.Unicode.GetBytes(strSource)
myFileStream.Write(bteEndode, 0, bteEndode.Length)
myFileStream.Close()
End Sub
End Class
FileStream.Length
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim fs As System.IO.FileStream
Dim r As System.IO.BinaryReader
Dim buffer(100) As Char
Dim mylength As Long
fs = New System.IO.FileStream("test.txt", IO.FileMode.OpenOrCreate)
r = New System.IO.BinaryReader(fs)
mylength = fs.Length
If mylength > 100 Then
mylength = 100
End If
r.Read(buffer, 0, mylength)
Console.WriteLine(buffer)
r.Close()
fs.Close()
End Sub
End Class
Read a file by using SeekOrigin
Imports System.IO
Imports System.Text
Public Class Tester
Public Shared Sub Main
Dim myFileStream As FileStream
Dim bteWrite() As Byte
Dim intByte As Integer
Dim lngLoop As Long
Try
intByte = Encoding.ASCII.GetBytes("asdf").Length
ReDim bteWrite(intByte)
bteWrite = Encoding.ASCII.GetBytes("asdf")
myFileStream = File.OpenWrite("test.txt")
Dim myOrigin As SeekOrigin
Dim lngPosition As Long
lngPosition = 123
myOrigin = SeekOrigin.Begin
" myOrigin = SeekOrigin.Current
" myOrigin = SeekOrigin.End
myFileStream.Seek(lngPosition, myOrigin)
For lngLoop = 0 To intByte - 1
myFileStream.Seek(1, SeekOrigin.Current)
myFileStream.WriteByte(bteWrite(lngLoop))
Next
myFileStream.Close()
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
Read byte array and convert it to ASCII string
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim myFileStream As New FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim bteRead() As Byte
Dim strRead As String
Dim lngRead As Long
lngRead = myFileStream.Length
ReDim bteRead(lngRead)
myFileStream.Read(bteRead, 0, lngRead)
strRead = Encoding.ASCII.GetString(bteRead)
Console.WriteLine(strRead)
myFileStream.Close()
End Sub
End Class
Read byte array and convert it to Unicode string
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim myFileStream As New FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim bteRead() As Byte
Dim strRead As String
Dim lngRead As Long
lngRead = myFileStream.Length
ReDim bteRead(lngRead)
myFileStream.Read(bteRead, 0, lngRead)
strRead = Encoding.Unicode.GetString(bteRead)
Console.WriteLine(strRead)
myFileStream.Close()
End Sub
End Class
Read byte using FileStream
Imports System.IO
Imports System.Text
Public Class Tester
Public Shared Sub Main
Dim myFileStream As FileStream
Dim intByte As Integer
Dim lngLoop As Long = 0
Dim bteRead() As Byte
Try
myFileStream = File.OpenRead("test.txt")
ReDim bteRead(myFileStream.Length)
Do While Not intByte = -1
intByte = myFileStream.ReadByte()
If intByte <> -1 Then bteRead(lngLoop) = CByte(intByte)
lngLoop += 1
Loop
Console.WriteLine(Encoding.ASCII.GetString(bteRead))
myFileStream.Close()
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
Read into a buffer
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim f As System.IO.FileStream
Dim mylength As Integer
Dim i As Integer
Dim mybuffer(100) As Byte
f = New System.IO.FileStream("test.txt", IO.FileMode.Open, IO.FileAccess.Read)
f.Read(mybuffer, 0, 100)
For i = 0 To 99
Console.WriteLine(Chr(mybuffer(i)))
Next
f.Close()
End Sub
End Class
Save UTF8Encoding string to text file
Imports System.IO
Imports System.Text
public class Test
public Shared Sub Main
Dim file_stream As New FileStream("test.txt", FileMode.Create)
Dim bytes As Byte() = New UTF8Encoding().GetBytes("Hello world!")
file_stream.Write(bytes, 0, bytes.Length)
file_stream.Close()
End Sub
End class
Use FileStream to write byte array
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim fs As System.IO.FileStream
Dim buffer As String
Dim i As Integer
Dim myarray(100) As Byte
fs = New System.IO.FileStream("test.txt", IO.FileMode.OpenOrCreate)
For i = 0 To 99
myarray(i) = i
Next
fs.Seek(0, System.IO.SeekOrigin.End)
fs.Write(myarray, 0, 100)
fs.Close()
End Sub
End Class
Write Bytes using FileStream
Imports System.IO
Imports System.Text
Public Class Tester
Public Shared Sub Main
Dim myFileStream As FileStream
Dim bteWrite() As Byte
Dim intByte As Integer
Dim lngLoop As Long
Try
intByte = Encoding.ASCII.GetBytes("asdf").Length
ReDim bteWrite(intByte)
bteWrite = Encoding.ASCII.GetBytes("asdf")
myFileStream = File.OpenWrite("test.txt")
For lngLoop = 0 To intByte - 1
myFileStream.WriteByte(bteWrite(lngLoop))
Next
myFileStream.Close()
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Sub
End Class