1 / 22

Chapter 11 FiLE Input & Output

Chapter 11 FiLE Input & Output. Introduction to Computer Science Using Ruby . File Access: Reading & Writing. Files can be manipulated using File Input/Output (I/O) To access files, the built-in Ruby File Class is used. File contains multiple methods: open close gets puts.

mele
Download Presentation

Chapter 11 FiLE Input & Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. (c) 2012 Ophir Frieder et al Chapter 11 FiLE Input & Output Introduction to Computer Science Using Ruby

  2. File Access: Reading & Writing • Files can be manipulated using File Input/Output (I/O) • To access files, the built-in Ruby File Class is used File contains multiple methods: • open • close • gets • puts (c) 2012 Ophir Frieder et al

  3. File Access: Reading & Writing • The method File.open instantiates a new file object • Enables Ruby to read from or write to an existing or new file • The file object returned by File.open can then be used to access the file specified in the File.open statement (c) 2012 Ophir Frieder et al

  4. File Access: Reading & Writing • The following code shows how to open a file: my_file = File.open(file_name,access_mode) • The my_file variable is a File object that can now be used to interact with the file’s contents • The file_name is the name of the file in the system – it is highly system dependant (c) 2012 Ophir Frieder et al

  5. File Access: Reading & Writing The way my_filefunctions depends on what access mode is used. The two main modes are: Table 11.1: Some Access Modes (c) 2012 Ophir Frieder et al

  6. File Access: Reading & Writing • To read a line of characters from a file, call the gets method • gets will read a new line each time • Returns nil when it reaches the end of the file (c) 2012 Ophir Frieder et al

  7. Example 11.1: Sample Code for File Reading 1 file = File.open("foo.txt", "r") 2 whole_file = "" 3 4 while (input_line = file.gets) 5 whole_file += input_line 6 end 7 8 puts "Contents of input file:" 9 puts whole_file Note: Reads and prints the “foo.txt.” file (c) 2012 Ophir Frieder et al

  8. Will store the file to display 1 file = File.open("foo.txt", "r") 2 whole_file = "" 3 4 while (input_line = file.gets) 5 whole_file += input_line 6 end 7 8 puts "Contents of input file:" 9 puts whole_file Will store the file to display Opens the file for read access Prints out the file (c) 2012 Ophir Frieder et al

  9. File Access: Reading & Writing • Writing to a file is similar to reading from a file • Instead of using read mode, use “w” for writing access • To output text to a file, use a local variable and invoke the puts method file.puts(“text goes here.”) (c) 2012 Ophir Frieder et al

  10. Example 11.2: File Read/Write Example This code writes input into a file: 1 file_a = File.open("bar.txt", "w") 2 3 puts "Please enter a line of text" 4 line = gets 5 file_a.puts(line) 6 file_a.close 7 8 file_b = File.open("bar.txt", "r") 9 puts "Contents of file:" 10 puts file_b.gets (c) 2012 Ophir Frieder et al

  11. Takes text input from the user 1 file_a = File.open("bar.txt", "w") 2 3 puts "Please enter a line of text" 4 line = gets 5 file_a.puts(line) 6 file_a.close 7 8 file_b = File.open("bar.txt", "r") 9 puts "Contents of file:" 10 puts file_b.gets Opens file with write access Writes user input to file Closes file Instantiates a new file Outputs content of new file (c) 2012 Ophir Frieder et al

  12. File Reader Class • We can now define a class which encapsulates file reading • The next example encapsulates reading and displaying a file (c) 2012 Ophir Frieder et al

  13. Example 11.3: File Reader Class 1 class FileReader 2 3 def initialize(file_name) 4 @file = File.open(file_name, "r") 5 end 6 7 def read_file 8 whole_file = "" 9 while (input_line = @file.gets) 10 whole_file += input_line 11 end 12 13 return whole_file 14 end 15 16 def display 17 puts "Contents of input file:" 18 puts read_file 19 end 20 (c) 2012 Ophir Frieder et al

  14. Example 11.3 Cont’d 21 def close 22 @file.close 23 end 24 end (c) 2012 Ophir Frieder et al

  15. 1 class FileReader 2 3 def initialize(file_name) 4 @file = File.open(file_name, "r") 5 end 6 7 def read_file 8 whole_file = "" 9 while (input_line = @file.gets) 10 whole_file += input_line 11 end 12 13 return whole_file 14 end 15 16 def display 17 puts "Contents of input file:" 18 puts read_file 19 end 20 Basic loop reads file one line at a time Defines the constructor Defines the method Appends contents to whole_file Opens the file to read contents Returns contents of file Display method which outputs the contents (c) 2012 Ophir Frieder et al

  16. Method closes opened file 21 def close 22 @file.close 23 end 24 end (c) 2012 Ophir Frieder et al

  17. Example 11.4: File Writer Class 1 class FileWriter 2 3 def initialize(file_name) 4 @file = File.open(file_name, "w") 5 end 6 7 def write_line(output_line) 8 @file.puts(output_line) 9 end 10 11 def close 12 @file.close 13 end 14 end (c) 2012 Ophir Frieder et al

  18. Opens file with write access 1 class FileWriter 2 3 def initialize(file_name) 4 @file = File.open(file_name, "w") 5 end 6 7 def write_line(output_line) 8 @file.puts(output_line) 9 end 10 11 def close 12 @file.close 13 end 14 end Constructor Write line method Outputs line to file Closes file (c) 2012 Ophir Frieder et al

  19. Example 11.5: FileReader/FileWriter Example 1 require_relative"file_reader.rb" 2 require_relative"file_writer.rb" 3 4 fr = FileReader.new("input.txt") 5 fw = FileWriter.new("output.txt") 6 7 input = fr.read_file 8 fw.write_line(input) 9 10 fw.close 11 fr.close This example uses the FileReader and FileWriter classes to read a file, then writes its contents to another file (c) 2012 Ophir Frieder et al

  20. Imports classes Instances for the classes 1 require_relative"file_reader.rb" 2 require_relative"file_writer.rb" 3 4 fr = FileReader.new("input.txt") 5 fw = FileWriter.new("output.txt") 6 7 input = fr.read_file 8 fw.write_line(input) 9 10 fw.close 11 fr.close Reads the file Writes string to the file Closes the file (c) 2012 Ophir Frieder et al

  21. File Reader/Writer Example • Test the program using Example 11.6: Sample Input File Hello World! A mighty fine day for ruby programming! Computer Science is the best! (c) 2012 Ophir Frieder et al

  22. Summary • We described the basic file input and output operations • Note that Ruby allows other file operations that are not covered in these lectures. (c) 2012 Ophir Frieder et al

More Related