Ruby - Count text file line

  1. HOME
  2. Ruby
  3. File
  4. Output text file line by line

Introduction

The following code adds a variable, text, to collect the lines together:

Demo

text='' 
line_count = 0 #  w  w w  . j a v a2s  .co  m
File.open("main.rb").each do |line|  
  line_count += 1 
  text << line 
end

puts "#{line_count} lines"

Result

Related Topic

  • Output text file line by line
  • Read whole text file to memory
  • Count Text File Characters
  • Count characters excluding whitespace
  • Counting Words in Text File
  • Counting Sentences and Paragraphs in Text file
  • Standard Input and Output
  • Text Files Reading
  • Split file by ,
  • Output file byte by type
  • Read text file character by character
  • Read an I/O stream line by line using gets
  • Read entire file into an array, split by lines, using readlines
  • Read an arbitrary number of bytes from a file
  • Reading text file line by line using each
  • Read file in UTF8
  • Transcoding from one encoding to another encoding.

  • Previous
  • Next

HOME | Copyright © www.java2s.com 2016