Read a file in chunks in Ruby

后端 未结 5 1488
一个人的身影
一个人的身影 2020-12-15 08:09

I need to read a file in MB chunks, is there a cleaner way to do this in Ruby:

FILENAME=\"d:\\\\tmp\\\\file.bin\"
MEGABYTE = 1024*1024
size = File.size(FILEN         


        
5条回答
  •  借酒劲吻你
    2020-12-15 08:56

    You can use IO#each(sep, limit), and set sep to nil or empty string, for example:

    chunk_size = 1024
    File.open('/path/to/file.txt').each(nil, chunk_size) do |chunk|
      puts chunk
    end
    

提交回复
热议问题