-
Jul 25th, 2021,05:37 AM #1
Thread Starter
Addicted Member
[RESOLVED] Vb.cyberspace - Split large text file past number of lines
How can I split a single text file with yard lines into multiple smaller files of, for example, 300 lines ? I constitute two C# code, they feel good, just I'm not able to convert them to vb.cyberspace
Lawmaking:
using (Arrangement.IO.StreamReader sr = new Arrangement.IO.StreamReader("path")) { int fileNumber = 0; while (!sr.EndOfStream) { int count = 0; using (Organisation.IO.StreamWriter sw = new System.IO.StreamWriter("other path" + ++fileNumber)) { sw.AutoFlush = true; while (!sr.EndOfStream && ++count < 20000) { sw.WriteLine(sr.ReadLine()); } } } }
Lawmaking:
var reader = File.OpenText(infile); string outFileName = "file{0}.txt"; int outFileNumber = 1; const int MAX_LINES = 300; while (!reader.EndOfStream) { var writer = File.CreateText(cord.Format(outFileName, outFileNumber++)); for (int idx = 0; idx < MAX_LINES; idx++) { writer.WriteLine(reader.ReadLine()); if (reader.EndOfStream) break; } writer.Close(); } reader.Shut();
-
Jul 25th, 2021,05:49 AM #ii
Re: Vb.net - Carve up big text file by number of lines
Then what specific problems did yous take while converting? Did you make whatever effort at all? That first lawmaking snippet has ii using statements and two while loops, two declarations, a property setting and a method call. What exactly is preventing yous doing the aforementioned thing in VB? Apart from the fact that you should be able to make a pretty good fist of a manual conversion for yourself if you carp to try, there are enough of lawmaking converters available but that are of varying quality but can pretty much all handle uncomplicated code like that. At that place'south also the downloadable Instant VB that can handle extremely complex code and so will have no issue with that basic stuff. Basically, if you had fabricated a reasonable effort and so you could have done this yourself and you lot could have still washed some of it yourself fifty-fifty with an unreasonable effort.
-
Jul 25th, 2021,10:41 AM #3
Thread Starter
Addicted Member
Re: Vb.net - Split large text file by number of lines
I don't know C#, I tried with some code converters without success, they tin not convert them.
I take already solved the trouble with a much longer code, I would similar to understand how to reduce the code
Code:
Dim OriginalFilePath = "F:\Documenti\Visual Studio 2017\Projects\TextFileSplit.txt" Dim lines = IO.File.ReadAllLines(OriginalFilePath) Dim NumOfLines As Integer = lines.Length, LinesPerFile Every bit Integer = 300 Dim NumOfFiles As Integer = NumOfLines \ LinesPerFile + 1 Dim StartIndex As Integer Dim EndIndex As Integer Dim sb As New System.Text.StringBuilder For i = 0 To NumOfFiles - ane EndIndex = StartIndex + LinesPerFile - 1 If EndIndex >= NumOfLines - 1 And so EndIndex = NumOfLines - 1 Terminate If For index = StartIndex To EndIndex sb.AppendLine(lines(alphabetize)) Side by side Dim NewFilePath As String = "F:\Download\File" + i.ToString + ".txt" IO.File.WriteAllText(NewFilePath, sb.ToString) StartIndex = EndIndex + 1 sb.Articulate() Side by side
Last edited by patel45; Jul 25th, 2021 at ten:55 AM.
-
Jul 28th, 2021,01:36 PM #4
Re: Vb.net - Split large text file by number of lines
If you would similar to see a proven method to chunk files (and there is a slap-up deal more lawmaking as it's ported from a C# enterprise utility I wrote) see the following GitHub repository.
-
Jul 29th, 2021,01:53 PM #5
Thread Starter
Addicted Member
Re: Vb.internet - Split large text file by number of lines
-
Jul 30th, 2021,03:17 AM #six
Re: Vb.net - Split big text file by number of lines
divide a single text file with 1000 lines into multiple smaller files of, for instance, 300 lines
Why? I would consider a one thousand line file as 'pocket-size' ? What problems are y'all getting with a file of this size that requires information technology to exist divide on number of lines?
All communication is offered in good faith merely. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed equally Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.1.0)
-
Jul 30th, 2021,08:47 PM #seven
Re: Vb.net - Split large text file by number of lines
This web site converted the lawmaking in the OP pretty comfortably. It produced this:-
vbnet Code:
-
Using sr As StreamReader = New StreamReader("path")
-
Dim fileNumber As Integer = 0
-
While Not sr.EndOfStream
-
Dim count As Integer = 0
-
Using sw As StreamWriter = New StreamWriter("other path" & Threading.Interlocked.Increase(fileNumber))
-
sw.AutoFlush = True
-
While Not sr.EndOfStream AndAlso Threading.Interlocked.Increment(count) < 20000
-
sw.WriteLine(sr.ReadLine())
-
Stop While
-
End Using
-
Terminate While
-
End Using
Though I'd probably refactor all that Interlocked.Increment code.
-
Jul 31st, 2021,12:22 PM #8
Thread Starter
Fond Member
Re: Vb.net - Split large text file past number of lines
Thank you Niya, it runs well
-
Jul 31st, 2021,06:thirty PM #9
Re: Vb.net - Split large text file by number of lines
Code:
Dim fileLines() as String = IO.File.ReadAllLines("path") Dim AllLines(CInt(Math.Floor(fileLines.Length / 300)))() as Cord For x as integer = 0 to AllLines.GetUpperBound(0) AllLines(10) = fileLines.Accept(300).ToArray Adjacent
Edit: Actually in that location's an error... Lawmaking:
Dim fileLines() as String = IO.File.ReadAllLines("path") Dim AllLines(CInt(Math.Floor(fileLines.Length / 300)))() as String For x as integer = 0 to AllLines.GetUpperBound(0) AllLines(x) = fileLines.Skip(x * 300).Take(300).ToArray Side by side
Terminal edited by .paul.; Jul 31st, 2021 at 09:09 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2021,12:24 PM #x
Thread Starter
Addicted Member
Re: Vb.net - Split big text file by number of lines
Thanks Paul, very compact code, I modified it then
Code:
Dim OriginalFilePath = "F:\Documenti\Visual Studio 2017\Projects\TextFileSplit.txt" Dim LinesPerFile Every bit Integer = 20 Dim fileLines() Every bit String = IO.File.ReadAllLines(OriginalFilePath) Dim AllLines(CInt(Math.Floor(fileLines.Length / LinesPerFile)))() As Cord For i = 0 To AllLines.Length - ane AllLines(i) = fileLines.Skip(i * LinesPerFile).Take(LinesPerFile).ToArray Dim NewFilePath As String = "F:\Download\File" + i.ToString + ".txt" IO.File.WriteAllLines(NewFilePath, AllLines(i)) Adjacent
-
Aug 2nd, 2021,08:00 PM #eleven
Re: Vb.net - Split large text file past number of lines
Supervene upon this:
vb.net Code:
-
CInt(Math.Floor(fileLines.Length / LinesPerFile))
with this:
vb.internet Lawmaking:
-
fileLines.Length \ LinesPerFile
-
Aug 2nd, 2021,08:18 PM #12
Re: Vb.net - Dissever large text file by number of lines
As you�re merely writing it directly to new files� Code:
Dim OriginalFilePath = "F:\Documenti\Visual Studio 2017\Projects\TextFileSplit.txt" Dim LinesPerFile Equally Integer = twenty Dim fileLines() Equally String = IO.File.ReadAllLines(OriginalFilePath) For i = 0 To AllLines.Length - one Dim subsetLines() As String = fileLines.Skip(i * LinesPerFile).Accept(LinesPerFile).ToArray Dim NewFilePath As String = "F:\Download\File" + i.ToString + ".txt" IO.File.WriteAllLines(NewFilePath, subsetLines) Adjacent
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2d, 2021,08:59 PM #xiii
Re: Vb.net - Split large text file by number of lines
Given that nosotros are supposedly talking about large files here, I'd be tempted to avert calling ReadAllLines.
vb.net Code:
-
Dim inputFilePath = "file path here"
-
Dim outputFolderPath = "folder path here"
-
Dim linesPerFile = 20
-
Dim fileNumber = one
-
Dim lines As New List(Of String)(linesPerFile)
-
For Each line In File.ReadLines(inputFilePath)
-
lines.Add together(line)
-
If lines.Count = linesPerFile Then
-
Dim outputFilePath = Path.Combine(outputFolderPath, $"File{fileNumber}.txt")
-
File.WriteAllLines(outputFilePath, lines)
-
fileNumber += ane
-
lines.Clear()
-
End If
-
Adjacent
-
If lines.Count > 0 Then
-
Dim outputFilePath = Path.Combine(outputFolderPath, $"File{fileNumber}.txt")
-
File.WriteAllLines(outputFilePath, lines)
-
Terminate If
It'southward also worth noting that this:
vb.internet Lawmaking:
-
Dim subsetLines() Every bit String = fileLines.Skip(i * LinesPerFile).Take(LinesPerFile).ToArray
is going to enumerate the same assortment over and over once more. Information technology will only enumerate role of the assortment each fourth dimension but that part will get larger and larger. It would be more performant to create the target array ahead of time and telephone call Array.CopyTo and specify the department of the source array to copy. Not a large deal for small data sets but a potential result for big ones.
-
Aug 2d, 2021,10:13 PM #14
Re: Vb.net - Carve up large text file by number of lines
Originally Posted by jmcilhinney
Given that we are supposedly talking about big files here...
one thousand lines is a large file???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug second, 2021,10:29 PM #15
Re: Vb.net - Carve up large text file by number of lines
Originally Posted by .paul.
1000 lines is a large file???
Non really, although the OP did say "big text file". I guess it's but large in relation to the very pocket-size files they desire to end upwardly with. Still, if null else, it's practiced to know the dissimilar ways that y'all can approach something similar this and what the pros and cons of each option are.
0 Response to "Split Large Text File Into Multiple Files Java Reading"
Post a Comment