VB.NET -> reading file with streamreader and streamwriter

Hi,

I tried to open a file, read line after line, and after each line I wrote it in an output file (passing line content to a string and then to the output file)...

The results are strange... With ascii file, the content is the same but the file size isn't... and with files like mpeg, avi, zip, etc. the size and the content are different. i tried different type of encoding, but no result...

Could someone help me about this...

I want to use this kind of code to encrypt a file, encrypting it line after line...



Public Sub readWrite()
dim LineBuffer as string

Dim oFileStream As Stream = File.Open(FilePath, FileMode.Open, FileAccess.Read)
Dim oStreamReader As New StreamReader(oFileStream)

Dim oOutputStream As Stream = File.Open(OutPutFile,
FileMode.Create, FileAccess.Write)
Dim oStreamWriter As New StreamWriter(oOutputStream)

oStreamReader.BaseStream.Seek(0, SeekOrigin.Begin)

Do
LineBuffer = oStreamReader.ReadLine
oStreamWriter.Write(LineBuffer.ToString)
Loop Until oStreamReader.Peek() = -1

oOutputStream.Flush()
oFileStream.Close()
oOutputStream.Close()

End Sub ReadWrite

Thanks,

David Campillo

Comments

  • : Hi,
    :
    : I tried to open a file, read line after line, and after each line I wrote it in an output file (passing line content to a string and then to the output file)...
    :
    : The results are strange... With ascii file, the content is the same but the file size isn't... and with files like mpeg, avi, zip, etc. the size and the content are different. i tried different type of encoding, but no result...
    :
    : Could someone help me about this...
    :
    : I want to use this kind of code to encrypt a file, encrypting it line after line...
    :
    :
    :
    : Public Sub readWrite()
    : dim LineBuffer as string
    :
    : Dim oFileStream As Stream = File.Open(FilePath, FileMode.Open, FileAccess.Read)
    : Dim oStreamReader As New StreamReader(oFileStream)
    :
    : Dim oOutputStream As Stream = File.Open(OutPutFile,
    : FileMode.Create, FileAccess.Write)
    : Dim oStreamWriter As New StreamWriter(oOutputStream)
    :
    : oStreamReader.BaseStream.Seek(0, SeekOrigin.Begin)
    :
    : Do
    : LineBuffer = oStreamReader.ReadLine
    : oStreamWriter.Write(LineBuffer.ToString)
    : Loop Until oStreamReader.Peek() = -1
    :
    : oOutputStream.Flush()
    : oFileStream.Close()
    : oOutputStream.Close()
    :
    : End Sub ReadWrite
    :
    : Thanks,
    :
    : David Campillo





    Hi, just another way to do it.

    Public Sub readWrite()

    : Dim LineBuffer as string
    :
    : : Dim oStreamReader As StreamReader()
    oStreamReader=IO.File.Open(FilePath)
    :
    : : Dim oStreamWriter As StreamWriter()
    : oStreamWriter=IO.File.AppendText(FilePath)

    :
    : LineBuffer = oStreamReader.ReadLine

    : Do Until oStreamReader = Nothing

    : LineBuffer = oStreamReader.ReadLine
    : oStreamWriter.Write(LineBuffer)
    : Loop

    :
    : oStreamReader.Close()
    : oStreamWriter.Close()
    :
    : End Sub ReadWrite

    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion