Write Data To File

H

Husam

Hi EveryBody:

How can I write a byte array to FileStream, I tried to do so and I use the
following code:
Dim rawan As New List(Of Byte())
Dim msg() As Byte = CType(rawan(TextBox1.Text), Byte())
Dim File_Name As String = "Winter.jpg"
Dim fs As New FileStream(File_Name, FileMode.CreateNew)
' Create the writer for data.
Dim w As New BinaryWriter(fs)
' Write data to Test.data.
Dim i As Integer
For i = 0 To msg.Length
w.Write(CInt(i))
Next i
w.Close()
fs.Close()

But I got the following message Can not can't not access file. So how can I
write my data to file or file stream? any help will be appreciated

regard's

Husam
 
O

\(O\)enone

Husam said:
How can I write a byte array to FileStream, I tried to do so and I
use the following code:
[...]

If you're using VS2005 or later (you didn't say if you were) and you want to
write a byte array to a file, this is an easier solution:

\\\
IO.File.WriteAllBytes("Winter.jpg", msg)
///

HTH,
 
F

Family Tree Mike

(O)enone is right, but in your code you don't have any Open calls on your
stream or writer. That would be necessary.
 
C

Cor Ligthert[MVP]

Husam,

Can you have a look what happens when you set option Strict to on, I have
the idea that you want to write nothing.

Cor
 
H

Herfried K. Wagner [MVP]

(O)enone said:
How can I write a byte array to FileStream, I tried to do so and I
use the following code:
[...]

If you're using VS2005 or later (you didn't say if you were) and you want
to write a byte array to a file, this is an easier solution:

\\\
IO.File.WriteAllBytes("Winter.jpg", msg)
///

.... or 'My.Computer.FileSystem.WriteAllBytes', which does pretty much the
same as the above.
 
G

Guru

Herfried K. Wagner said:
(O)enone said:
How can I write a byte array to FileStream, I tried to do so and I
use the following code:
[...]

If you're using VS2005 or later (you didn't say if you were) and you want
to write a byte array to a file, this is an easier solution:

\\\
IO.File.WriteAllBytes("Winter.jpg", msg)
///

... or 'My.Computer.FileSystem.WriteAllBytes', which does pretty much the
same as the above.

What a pair of morons you two are. He's using a BinaryWriter, in case you
hadn't noticed.
 
G

Guru

Husam said:
Hi EveryBody:

How can I write a byte array to FileStream, I tried to do so and I use the
following code:
Dim rawan As New List(Of Byte())
Dim msg() As Byte = CType(rawan(TextBox1.Text), Byte())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rawan(Of Byte())
contains nothing. I will assume that "I use the following code:" is a
little, white lie.
Dim File_Name As String = "Winter.jpg"
Dim fs As New FileStream(File_Name, FileMode.CreateNew)
' Create the writer for data.
Dim w As New BinaryWriter(fs)
' Write data to Test.data.
Dim i As Integer
For i = 0 To msg.Length
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 byte too many. The byte array is
zero-based, whereas the .Length property is unary, base-1; you must subtract
1 from .Length. Since you neglected to state when or where in the loop the
problem actually occurs, I will assume that your mistake there is the
probable cause of the FileStream complaining.

Funny how the allegedly expert MVPs didn't notice that.
w.Write(CInt(i))
Next i
w.Close()
fs.Close()

But I got the following message Can not can't not access file. So how can
I write my data to file or file stream? any help will be appreciated

There is no need to either explicitly create a FileStream object or write
every individual byte from within a loop.

Dim w As New BinaryWriter(File.Open("Winter.jpg", FileMode.Create))
w.Write(msg)
w.Close()

The BinaryWriter will automatically close the undeclared FileStream object.

HTH
 
H

Herfried K. Wagner [MVP]

Guru said:
How can I write a byte array to FileStream, I tried to do so and I
use the following code:
[...]

If you're using VS2005 or later (you didn't say if you were) and you
want to write a byte array to a file, this is an easier solution:

\\\
IO.File.WriteAllBytes("Winter.jpg", msg)
///

... or 'My.Computer.FileSystem.WriteAllBytes', which does pretty much the
same as the above.

What a pair of morons you two are. He's using a BinaryWriter, in case you
hadn't noticed.

There is no reason to use 'BinaryWriter' in this particular case.
 
G

Guru

Herfried K. Wagner said:
Guru said:
How can I write a byte array to FileStream, I tried to do so and I
use the following code:
[...]

If you're using VS2005 or later (you didn't say if you were) and you
want to write a byte array to a file, this is an easier solution:

\\\
IO.File.WriteAllBytes("Winter.jpg", msg)
///

... or 'My.Computer.FileSystem.WriteAllBytes', which does pretty much
the same as the above.

What a pair of morons you two are. He's using a BinaryWriter, in case you
hadn't noticed.

There is no reason to use 'BinaryWriter' in this particular case.

That is not your decision to make, it is the OP's.
 
H

Herfried K. Wagner [MVP]

Guru said:
That is not your decision to make, it is the OP's.

It's simply a bad idea to use 'BinaryWriter' if you want to persist an array
of bytes forming a file's contents.
 
G

Guru

Herfried K. Wagner said:
It's simply a bad idea to use 'BinaryWriter' if you want to persist an
array of bytes forming a file's contents.

Really? And you expect me to just believe that without you needing to
support your claim, do you?

"The BinaryWriter and BinaryReader classes are used for writing and reading
data, rather than character strings."

.NET Framework Developer's Guide
How to: Read and Write to a Newly Created Data File

Perhaps you should have a word with with Microsoft and correct the software
designers, eh?

Perhaps it's just that you're to damned pigshite-thick to realise he was
writing jpeg data, you thick-as-two-short-planks macaroon.
 
H

Herfried K. Wagner [MVP]

Guru said:
Really? And you expect me to just believe that without you needing to
support your claim, do you?

"The BinaryWriter and BinaryReader classes are used for writing and
reading
data, rather than character strings."

.NET Framework Developer's Guide
How to: Read and Write to a Newly Created Data File

That's exactly what the methods mentioned by (O)enone and me do. However,
note that these methods were introduced in .NET 2.0/VB 2005.
 
G

Guru

Herfried K. Wagner said:
That's exactly what the methods mentioned by (O)enone and me do. However,
note that these methods were introduced in .NET 2.0/VB 2005.

And your breakthrough point being...? That you're stuck in the past and can
only talk in terms of .NET 1.1?
 
H

Herfried K. Wagner [MVP]

Guru said:
And your breakthrough point being...? That you're stuck in the past and
can only talk in terms of .NET 1.1?

The article you referred to (and your opinion which seems to be based on
that) is stuck in the past. Instead of instantiating objects and calling
multiple methods, there are now at least two ways to archieve the same
result by calling a single method!
 
G

Guru

Herfried K. Wagner said:
The article you referred to (and your opinion which seems to be based on
that) is stuck in the past.

Oh look! An IKYABWAI lame.
Instead of instantiating objects and calling multiple methods, there are
now at least two ways to archieve the same result by calling a single
method!

As for the value of your opinion, it isn't worth the paper it's not printed
on.
 
C

Cor Ligthert[MVP]

Herfried,

What did you often write, I have showed it you by email lately, although I
could not resist a minute ago a reply to the troll, let us think about that.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top