Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to read Textfile FAST into memory and then read LINE

How to read Textfile FAST into memory and then read LINE

Scheduled Pinned Locked Moved C#
questiondata-structuresperformancetutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    error1408
    wrote on last edited by
    #1

    Hi, have to parse a big Textfile with about 33000 lines and at the moment this is very slow. A colleague advised me to read the file into a buffer (byte Array) and than parse it. Thats ok, but i need the "readline" functionality. How can i read the buffer line-by-line like the Streamreader? I'm sure someone knows :-D

    P W 2 Replies Last reply
    0
    • E error1408

      Hi, have to parse a big Textfile with about 33000 lines and at the moment this is very slow. A colleague advised me to read the file into a buffer (byte Array) and than parse it. Thats ok, but i need the "readline" functionality. How can i read the buffer line-by-line like the Streamreader? I'm sure someone knows :-D

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I guess you could always convert the byte array into a string and then split it based on Environment.NewLine. This sounds as though it wouldn't be very performant either.

      Deja View - the feeling that you've seen this post before.

      1 Reply Last reply
      0
      • E error1408

        Hi, have to parse a big Textfile with about 33000 lines and at the moment this is very slow. A colleague advised me to read the file into a buffer (byte Array) and than parse it. Thats ok, but i need the "readline" functionality. How can i read the buffer line-by-line like the Streamreader? I'm sure someone knows :-D

        W Offline
        W Offline
        woudwijk
        wrote on last edited by
        #3

        you could use FileStream to copy the file into a MemoryStream, and later use StreamReader from MemoryStream to parse it. FileStream fs = new FileStream("text.txt", FileMode.Open, FileAccess.Read); MemoryStream ms = new MemoryStream(); byte[] buf = new byte[4096]; int bytes = 0; while((bytes = fs.Read(buf, 0, 4096)) > 0) { ms.Write(buf, 0, bytes); } fs.close(); StreamReader sr = new StreamReader(ms); string x = sr.ReadLine();

        My second computer is your linux box.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups