我需要以MB块的形式读取一个文件,有没有更简洁的方法在Ruby中执行此操作:
FILENAME="d:\\tmp\\file.bin"
MEGABYTE = 1024*1024
size = File.size(FILENAME)
open(FILENAME, "rb") do |io|
read = 0
while read < size
left = (size - read)
cur = left < MEGABYTE ? left : MEGABYTE
data = io.read(cur)
read += data.size
puts "READ #{cur} bytes" #yield data
end
end
Run Code Online (Sandbox Code Playgroud)
mba*_*hau 20
改编自Ruby Cookbook第204页:
FILENAME = "d:\\tmp\\file.bin"
MEGABYTE = 1024 * 1024
class File
def each_chunk(chunk_size = MEGABYTE)
yield read(chunk_size) until eof?
end
end
open(FILENAME, "rb") do |f|
f.each_chunk { |chunk| puts chunk }
end
Run Code Online (Sandbox Code Playgroud)
免责声明:我是一个红宝石新手,并没有测试过这个.
Phr*_*ogz 10
或者,如果你不想monkeypatch File:
until my_file.eof?
do_something_with( my_file.read( bytes ) )
end
Run Code Online (Sandbox Code Playgroud)
例如,将上载的临时文件流式传输到新文件中:
# tempfile is a File instance
File.open( new_file, 'wb' ) do |f|
# Read in small 65k chunks to limit memory usage
f.write(tempfile.read(2**16)) until tempfile.eof?
end
Run Code Online (Sandbox Code Playgroud)
您可以使用IO#each(sep, limit), 并设置sep为nil或空字符串,例如:
chunk_size = 1024
File.open('/path/to/file.txt').each(nil, chunk_size) do |chunk|
puts chunk
end
Run Code Online (Sandbox Code Playgroud)