Binary file I/O using methods of streamreader and streamwriter

  • Thread starter Thread starter David Buchan
  • Start date Start date
D

David Buchan

Hello,

I wonder if anyone could help me.

I'm using vb.NET and I'd like to read a binary file, byte by byte, and
then write to another file (making a duplicate, identical file).

I'd then like to modify the program to take the integer value of each
byte and say, add or subtract an integer from it, with the result
still being an integer from 0 to 255. Then write another file that is
just a list of the integers (as a column of numbers).

I think that vb6 allowed the use of GET and PUT, but I don't think
vb.NET allows those anymore. I believe I have to use the methods of
streamReader and streamWriter to accomplish the task, but I don't know
how.

Thanks,
Dave
 
* (e-mail address removed) (David Buchan) scripsit:
I wonder if anyone could help me.

I'm using vb.NET and I'd like to read a binary file, byte by byte, and
then write to another file (making a duplicate, identical file).

I'd then like to modify the program to take the integer value of each
byte and say, add or subtract an integer from it, with the result
still being an integer from 0 to 255. Then write another file that is
just a list of the integers (as a column of numbers).

I think that vb6 allowed the use of GET and PUT, but I don't think
vb.NET allows those anymore. I believe I have to use the methods of

In VB.NET, they are called 'FileGet' and 'FilePut'.
streamReader and streamWriter to accomplish the task, but I don't know
how.

Alreadly had a look at the 'Read', 'ReadLine', 'Write', 'WriteLine'
methods and their overloaded versions?
 
David,
If you are operating strictly byte reads & writes all you need is Stream or
FileStream.

I would use something like:

Dim inputPath As String
Dim outputPath As String

Dim input As New FileStream(inputPath, FileMode.Open,
FileAccess.Read)
Dim output As New FileStream(outputPath, FileMode.Create,
FileAccess.Write)

Dim count As Integer = 1024
Dim buffer(count - 1) As Byte

count = input.Read(buffer, 0, count)
Do Until count = 0
' modify each byte in buffer here
output.Write(buffer, 0, count)
count = input.Read(buffer, 0, count)
Loop

input.Close()
output.Close()


StreamReader & StreamWriter are more useful when you want to translate a
text stream to/from "primitive" values, such as Integer, Double, and String.

BinaryReader & BinaryWriter are useful when you want to translate a binary
stream to/from "primitive" values, such as Integer, Double, and String.

Hope this helps
Jay
 
If you want to copy a file you can use "File" class from System.IO
E.x) File.Copy("SourceFileName", "DestFileName")

Now, if you want to work with the bytes of a file you can use "FileStream"
class
from the same namespace.
E.x) Dim FS as new Filestream("FileName",Mode)
Dim Buffer() as byte

'Get the bytes from file to a byte array
redim Buffer(FS.Length-1)
FS.Read(Buffer,0,Buffer.Length)

'Do your stuff... :-)
For i as int32=0 to Buffer.Length-1
Buffer(i) = '...... Your code.
next

'Save the byte array and close the stream.
FS.Position=0
FS.Write(Buffer,0,Buffer.length)
FS.Close

I hope this works for you...
George
 
Thanks guys.

FileStream looks like just the ticket.

I appreciate all the responses.

Dave
 
Back
Top