Question Reading file and write it to new file

Renoald

New member
Joined
Nov 5, 2010
Messages
3
Programming Experience
Beginner
Hai , I have a problem and hope someone have idea to rsolve it.
I have a file and inside the file the format is like that :
123.567 456.789 12.345 223 223 221 223
123.555 456.788 12.344 223 223 221 222
123.456 345.678 456.234 445 445 445 445
'''''''
total line is 3456789903
I want use vb console application to read the line and every 130000 line create a new output file.
any one have idea about that ?
 

rcombs4

Well-known member
Joined
Aug 6, 2008
Messages
189
Programming Experience
3-5
Just break it down step by step.

1. Find the file
2. build a loop to read a line at a time until you are at the end of the file
3. Read a line
4. write a line
5. at #130000 create a new file
 

Renoald

New member
Joined
Nov 5, 2010
Messages
3
Programming Experience
Beginner
This is my Code , but i think not working

Just break it down step by step.

1. Find the file
2. build a loop to read a line at a time until you are at the end of the file
3. Read a line
4. write a line
5. at #130000 create a new file

'This is my code
Dim read As New FileStream(myfile, FileMode.Open, FileAccess.Read)
Dim re As New StreamReader(read)
Dim C as integer
While re.Peek() > 0
value = re.ReadLine()
C = C + 1
if C > 130000
'create new file
Dim r As New FileStream(myfile, FileMode.Create, FileAccess.Write)
Dim R1 As New StreamWriter(r)
r.writeline(value)
????????????
End While


?????problem is here
how i create new file in while loop ?
there are total line/130000 new file to create and store data:
each 130000 line write to a new file.........
how about before 130000 line?
since each loop if i not change the file name , the output will overwrite......
can "Dim r As New FileStream(myfile, FileMode.Create, FileAccess.Write)"
do it in array file , if not i think i need to write thosands of this statement in my code!
Thank You for advice
 

rcombs4

Well-known member
Joined
Aug 6, 2008
Messages
189
Programming Experience
3-5
Try putting your FileStream and StreamWriter outside the loop. that way on the 130000th line you can close the file and open a new one. Just gotta figure out how to change file name inside the loop.
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
Looks like you've put forth some effort getting this solved.

1. Create a StreamReader to read the original file line by line.
2. Create a StreamWriter to write out each individual partial file.
3. Read a line from the original file.
3a. If your line counter is less than your cutoff point then write it to the current output file and increment the line counter.
3b. Otherwise close your partial file and create a new one. Reset your line counter. Write the line to the new file.
4. Repeat until the end of the original file.

VB.NET:
        Dim partCount As Integer = 1

        Using sr As New IO.StreamReader("C:\Temp\PartialSplit\Original.txt")
            Dim sw As New IO.StreamWriter("C:\Temp\PartialSplit\Part1.txt")

            Dim lineCount As Integer = 0
            While sr.Peek <> -1

                If lineCount < 1000 Then
                    sw.WriteLine(sr.ReadLine)
                    lineCount += 1
                Else
                    sw.Close()
                    partCount += 1
                    sw = New IO.StreamWriter(String.Format("C:\Temp\PartialSplit\Part{0}.txt", partCount))
                    sw.WriteLine(sr.ReadLine)
                    lineCount = 1
                End If

            End While

            sw.Close()
        End Using
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I always favoured this style:

VB.NET:
        Dim partCount As Integer = 1

        Using sr As New IO.StreamReader("C:\Temp\PartialSplit\Original.txt")
            Dim sw As New IO.StreamWriter("C:\Temp\PartialSplit\Part1.txt")

            Dim lineCount As Integer = 0
            While sr.Peek <> -1

                If lineCount > 130000 Then
                    sw.Close()
                    partCount += 1
                    sw = New IO.StreamWriter(String.Format("C:\Temp\PartialSplit\Part{0}.txt", partCount))
                    lineCount = 1
                End If

                sw.WriteLine(sr.ReadLine)
                lineCount += 1

            End While

            sw.Close()
        End Using
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,552
Location
Norway
Programming Experience
10+
I always favoured this style:
how about :
VB.NET:
Do Until reader.EndOfStream
or
VB.NET:
While Not reader.EndOfStream
 
Top Bottom