Ruby learning files and databases (1)

Source: Internet
Author: User

Input and Output:

Standard input:

A = gets, gets is to get a row of data from the standard input, lines = readlines get multiple lines of content at a time until EOF (CTRL + D)

Puts a, puts is printed to the standard output

 

File input and output:

File class to open files, can be plain text and binary files

File. Open ("text.txt"). Each {| Line | puts line}, file. Open accepts the code block. When the code block ends, the file is automatically closed.

You can also file. New ("text.txt", "R"). Each {| Line | puts line}, file. New returns the file object, and file. Close must be used to close the file.

class MyFile  attr_reader :handle  def initialize(filename)    @handle = File.new(filename,"r")  end    def finished    @handle.close  end  endf = MyFile.new("D://rb//text.txt")puts f.handle.each{|txt| puts txt}f.finished

The default each identifier is "\ n", which is defined as "and ".

Use the each_byte method to read the I/O Stream in a verbatim manner. Use the CHR method to convert to character ~

 

In addition, use the gets method to read data.

  def self.readf    File.open("D://rb//text.txt") do |f|      10.times{ puts f.gets}    end  end

At the same time, it is acceptable that the delimiter F. Gets (',') and F. GETC is the non-iterative version of each_byte.

 

Whole row read

  def self.readl    puts File.open("D://rb//text.txt").readlines.join("--")  end

Read arbitrary bytes

  def self.readb    File.open("D://rb//text.txt") do |f|      puts f.read(6)    end  end

Recommended Methods ~

File. Read (filename) and file. readlines (filename)

 

Input:

File.open("text.txt","w") do |f|f.puts("this is a test")end

File. New File Mode

File Mode Operation
R Read-only, starting with a file pointer
R + Readable and writable, starts with a file pointer, and rewrites
W Write and rewrite
W + Readable and writable, creating new files
A Write (additional mode)
A + End of the readable and writable (additional mode) file pointer

Gets --> puts

GETC --> putc

Read --> write

Renamed: file. Rename ("file1", "file2 ")

Delete: file. Delete ("file1", "file2 ".....)

The comparison is the same: file. identical? ("File1", "file2 ")

Organization File Path strength: file.join('full', 'path', 'here', 'filename.txt ')

Last File Modified: file. mtime ("file1 ")

Whether the file exists: file. exist ("file1 ")

File Size: file. Size ("file1 ")

End of the file: f. EOF

Location of zhizhenjiang: Seek Method
Seek three modes

   
IO: seek_cur Move n Bytes forward from current position

IO: seek_end

Move to a certain position at the end of the file. This indicates that the search starts from the end and may use a negative number.
IO: seek_set Move to the absolute position of the file. Exactly the same as Pos =
  def self.filepoint    f = File.new("D://rb//text.txt","r+")    f.seek(-5,IO::SEEK_END)    f.putc "X"    f.close  end

 

Directory processing: (File: separator is a slash)

Display current directory: Dir. pwd

Move the current directory: Dir. chdir ("/usr/bin ")

Obtain the directory file list: Dir. Entries ("/usr/bin"). Join (''). An array is returned. The same applies to Dir. foreach. Dir ["/usr/bin/*"]

Create directory: Dir. mkdir ("dir ")

Delete Directory: Dir. Delete ("dir ")

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.