Split Large Text File Into Multiple Files Java Reading

  1. #1

    patel45 is offline

    Thread Starter

    Addicted Member


    Resolved [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();

  2. #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.

  3. #3

    patel45 is offline

    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.

  4. #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.

  5. #5

    patel45 is offline

    Thread Starter

    Addicted Member


    Re: Vb.internet - Split large text file by number of lines


  6. #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)


  7. #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:

                                    
    1. Using sr As StreamReader = New StreamReader("path")

    2.     Dim fileNumber As Integer = 0

    3.     While Not sr.EndOfStream

    4.         Dim count As Integer = 0

    5.         Using sw As StreamWriter = New StreamWriter("other path" & Threading.Interlocked.Increase(fileNumber))

    6.             sw.AutoFlush = True

    7.             While Not sr.EndOfStream AndAlso Threading.Interlocked.Increment(count) < 20000

    8.                 sw.WriteLine(sr.ReadLine())

    9.             Stop While

    10.         End Using

    11.     Terminate While

    12. End Using


    Though I'd probably refactor all that Interlocked.Increment code.

  8. #8

    patel45 is offline

    Thread Starter

    Fond Member


    Re: Vb.net - Split large text file past number of lines

    Thank you Niya, it runs well

  9. #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:



  10. #x

    patel45 is offline

    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

  11. #eleven

    Re: Vb.net - Split large text file past number of lines

    Supervene upon this:

    vb.net Code:

                                    
    1. CInt(Math.Floor(fileLines.Length / LinesPerFile))

    with this:

    vb.internet Lawmaking:

                                    
    1. fileLines.Length \ LinesPerFile


  12. #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:



  13. #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:

                                    
    1. Dim inputFilePath = "file path here"

    2. Dim outputFolderPath = "folder path here"

    3. Dim linesPerFile = 20

    4. Dim fileNumber = one

    5. Dim lines As New List(Of String)(linesPerFile)

    6. For Each line In File.ReadLines(inputFilePath)

    7.     lines.Add together(line)

    8.     If lines.Count = linesPerFile Then

    9.         Dim outputFilePath = Path.Combine(outputFolderPath, $"File{fileNumber}.txt")

    10.         File.WriteAllLines(outputFilePath, lines)

    11.         fileNumber += ane

    12.         lines.Clear()

    13.     End If

    14. Adjacent

    15. If lines.Count > 0 Then

    16.     Dim outputFilePath = Path.Combine(outputFolderPath, $"File{fileNumber}.txt")

    17.     File.WriteAllLines(outputFilePath, lines)

    18. Terminate If

    It'southward also worth noting that this:

    vb.internet Lawmaking:

                                    
    1. 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.

  14. #14

    Re: Vb.net - Carve up large text file by number of lines

    Quote Originally Posted by jmcilhinney View Post

    Given that we are supposedly talking about big files here...

    one thousand lines is a large file???


    • Coding Examples:
    • Features:
    • Online Games:
    • Compiled Games:



  15. #15

    Re: Vb.net - Carve up large text file by number of lines

    Quote Originally Posted by .paul. View Post

    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.

Posting Permissions

  • Y'all may non post new threads
  • You may non post replies
  • You may non post attachments
  • You lot may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] lawmaking is On
  • HTML code is Off

Click Hither to Expand Forum to Full Width

hooverrevillon.blogspot.com

Source: https://www.vbforums.com/showthread.php?892723-RESOLVED-Vb-net-Split-large-text-file-by-number-of-lines

0 Response to "Split Large Text File Into Multiple Files Java Reading"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel