read and write to file

otuatail

Active member
Joined
May 25, 2011
Messages
28
Programming Experience
5-10
Hi New to this group and a junior .net developer. Have had lots of experiance in development including VB from version 1 20 years ago now.

I am strugling with vb.net. I can read from a file but not able to establish a writing object. what I have so far is this.

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.IO
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Text
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] EuroConverter
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1.Click
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] io [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.IO.File
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Reader [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] IO.StreamReader
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Writer [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] IO.StreamWriter
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sLine [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] values [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]()
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sWrite [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'sOutput = txtOutput.Text
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Reader = io.OpenText(txtInput.Text)
Writer = io.Create(txtOutput.Text)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Do
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]sLine = Reader.ReadLine
values = sLine.Split([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","c[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
values(1) = Trim(values(1))
values(5) = Trim(values(5))
values(6) = Trim(values(6))
values(7) = Trim(values(7))
values(8) = Trim(values(8))
values(9) = Trim(values(9))
values(10) = Trim(values(10))
values(11) = Trim(values(11))
values(12) = Trim(values(12))
values(13) = Trim(values(13))
values(1) = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"'"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + values(1) + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"'"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]values(3) = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"'"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + values(3) + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"'"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Now re-build
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]sLine = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Join([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], values)
sWrite = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"INSERT into EuroMillions Values ("[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + sLine + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]");"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Now re-build[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Until[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sLine [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]sr.Close()
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE][/COLOR][/SIZE]


TIA

Paul
 
additional info

What I wanted to do was
Dim Reader As StreamReader
Dim Writer As StreamWriter

Then initialise. Searching the internet only has Dim with New objects
 
No need for that IO.Create statement you can create and open the streamwriter in one line like so:

Dim sw as New System.IO.StreamWriter("C:\Output.txt")
Dim line as string = "This is the text that will appear on line 1"
sw.writeline(line)
sw.close()
 
Moved but not told where it is moved to or a linkk to it

I am perfectly aware that I can do it all in one line but I am also aware I can do it in two or more lines. As I stated you can re-use objects. I want to create a StreamWriter object and re-use it not use the NEW. It makes sence so how do I do this and what is the meethod. I have searched the entire web and can not find this information.

 
I am not sure exactly what you are trying to accomplish, tells us what you want to accomplish we will tell you the best way. To create a new instance of a streamwriter you use the new keyword, or you can retrieve an instance from some function that returns a streamwriter. But don't forget you will have to access these when you want to close them. Possibly use a collection of streamwriters, but again would be great to know what you are trying to do.
 
most objects can be created and initialized seperatly can't they

Dim Reader As IO.StreamReader
Dim Writer As IO.StreamWriter
// I now have 2 objects
Reader = io_OpenText(txtInput.Text)
// I have initialized this object to point at a file
// I can change this if I want ?
// Problem how do I initialize an already created StreamWriter object.
 
Dim sw as New System.IO.StreamWriter("C:\Output.txt")
Dim line as string = "This is the text that will appear on line 1"
sw.writeline(line)
sw.close()
sw = New System.IO.StreamWriter("C:\Output2.txt")
sw.writeline(line)
sw.close()
sw = New System.IO.StreamWriter("C:\Output3.txt")
sw.writeline(line)
sw.close()
 
Yes I see that. My SIMPLE question was, WHY does this work.
Dim Reader As IO.StreamReader
Reader = io_OpenText(txtInput.Text)
// by all accounts this should fail as it initialised seperatly

Dim sr As StreamReader = New StreamReader("TestFile.txt")
// This is the ONLY way it will work. ALL objects MUST be initialised at creation in .NET
 
Ok I have done it this way

I would have prefered Create, Reuse,Reuse,Reuse
instead of
Create,Destroy,Create,Destroy,Create,Destroy
Hope the garbage collector can keep up. I thought that re-use of objects was better.

Can you point me in the direction for the forum that deals with
package and deployment as I was unable to get this to work before.
 
Dim Reader As IO.StreamReader
Reader = io_OpenText(txtInput.Text)
// by all accounts this should fail as it initialised seperatly
First line is a declaration of a variable, it simply specifies variable name and type. The default variable value, since this is a reference type, is Nothing. By declaring a variable memory is allocated for any value that variable can hold, for reference types only storage for memory address is allocated.

Second line is assigning a value to the variable, ie the StreamReader object created by OpenText function. Memory is allocated here for the object, and the memory address is assigned the variable. Objects are always created and initialized using the New keyword that invokes the constructor, or a function the does this implicitly.

Additional note, it is recommended, when object can be created and assigned within same scope as variable is declared, to use the shorthand form of 'Dim variablename As New something' to perform all three operations in one. This is thus short for the single line code 'Dim variablename As something = New something'. As explained, this is recommended when can be applied, but not a requirement.

You can of course reuse the object all you want, for example by declaring the variable outside a loop you can reuse the object for every iteration of the loop. Another example of reuse is if you declare the variable outside the scope of a local method, ie as a private class field, then you can reuse the object each time a class method is called by referring to that variable. Just beware to close the object when finished using it, which can be more difficult to manage if you keep it in class scope. Narrowest scope is the better OOP practice, and may be more important that object creation cost. Yet another example of reuse is to create the object in a local method, but pass it on as parameter to other methods called. This is what ss7thirty was asking you to clarify, by not explaining what you're are doing or what you mean it is impossible to provide any specific help.
 
The point I am trying to make is
Dim Reader As IO.StreamReader
Reader = io_OpenText("fileA.txt")
Do something
Reader = io_OpenText("fileB.txt")
Do something
Reader = io_OpenText("fileC.txt")
Do something
Reader.Close() // one object re-used
What I was trying to ask but nobody is able to answer is if I do this
Dim Writer As IO.StreamWriter
Do something
'method for asigning an output source
Do something
'method for asigning an output source
Do something
Reader.Close()
If I asked how to build a rocket to go to the moon, I would expect someone to tell me what fuel I needed, not ask me why I wanted to go.
If I had to write to 25,000 files one after the other. This would mean 25,000 objects created and destroyed, taking up large amounts of memory because the garbage collection has not had time to free up memory. This is why I have been asking the question.
 
You can't change the Stream for an existing StreamReader, and neither change the underlying source of an existing Stream, if that is what you are attempting to ask.
Garbage collection happens when it needs to, as long as you close/dispose or by other means free the resources, the garbage can be collected.
 
Back
Top