Ruby vs Java – Read and Write Text Files

This post demonstrates how to read and write text files in several scenarios. It makes some short references to equivalent Java concepts and codes.

Ruby, Java, and Others

Here are some information about the environment used for this Ruby post.

  • Windows 10
  • Windows command line
  • Notepad++
  • Ruby for Windows
  • Java 8 – JDK
  • Eclipse Mars

Reading and Writing Files

Reading and writing files in Ruby requires the use of the Class IO which is part of Ruby’s Core API. Think of it like the Java IO package.

Read and Write in Bulk

In Ruby

In Java

The equivalent Java codes are as follow.

Read and Write line by line

In Ruby

In Java