Ruby common file Manipulation methods _ruby topics

Source: Internet
Author: User
Tags require

One, new file

Copy Code code as follows:

F=file.new (File.join ("C:", "Test.txt"), "w+")
F.puts ("I am Jack")
F.puts ("Hello World")

File mode
"R": Read-only. Starts at beginning of file (default mode).
"r+": Read-write. Starts at beginning of file.
"W": Write-only. Truncates existing file to zero length or creates a new file for writing.
"w+": Read-write. Truncates existing file to zero length or creates a new file for reading and writing.
"A": write-only. Starts at end of file if file exists; Otherwise, creates a new file for writing.
"A +": Read-write. Starts at end of file if file exists; Otherwise, creates a new file for reading and writing.
"B": (Dos/windows only.) Binary file mode. May appear and any of the key letters listed above

Second, read the file

Copy Code code as follows:

File=file.open (File.join ("C:", "Test.txt"), "R")
File.each {|line| print "#{file.lineno}.", line}
File.close

Third, new, delete, rename files
Copy Code code as follows:

File.new ("Books.txt", "W")
File.rename ("Books.txt", "Chaps.txt")
File.delete ("Chaps.txt")

Four, directory operation
1 Creating a directory
Copy Code code as follows:

Dir.mkdir ("C:/testdir")
#删除目录
Dir.rmdir ("C:/testdir")
#查询目录里的文件
P Dir.entries (File.join ("C:", "Ruby")). Join (")
#遍历目录
Dir.entries (File.join ("C:", "Ruby")). Each {
|e| Puts E
}

1, ARGV and ARGF
Copy Code code as follows:

Argv
ARGV << "Cnblogslink.txt"
#The gets method are a Kernel method this gets lines from ARGV
Print while gets
P Argv.class

Argf
While line = Argf.gets
Print Line
End


2. File Information Inquiry
Copy Code code as follows:

#文件是否存在
P file::exists? ("Cnblogslink.txt") # => True
#是否是文件
P file.file? ("Cnblogslink.txt") # => True
#是否是目录
P File::d irectory? ("C:/ruby") # => True
P File::d irectory? ("Cnblogslink.txt") # => False
#文件权限
P file.readable? ("Cnblogslink.txt") # => True
P file.writable? ("Cnblogslink.txt") # => True
P file.executable? ("Cnblogslink.txt") # => False
#是否是零长度
P File.zero? ("Cnblogslink.txt") # => False
#文件大小 bytes
P file.size? ("Cnblogslink.txt") # => 74
P File.size ("Cnblogslink.txt") # => 74
#文件或文件夹
P File::ftype ("Cnblogslink.txt") # => "File"
#文件创建, modification, last access time
P File::ctime ("Cnblogslink.txt") # => Sat Sep 19 08:05:07 +0800 2009
P File::mtime ("Cnblogslink.txt") # => Sat Sep 19 08:06:34 +0800 2009
P File::atime ("Cnblogslink.txt") # => Sat Sep 19 08:05:07 +0800 2009

3. Find Files
Copy Code code as follows:

Puts "Find all files and folders under directory"
dir["c:/ruby/*"].each {|x|
Puts X
}
Puts "conditional query"
Dir.foreach (' C:/ruby ') {
|x| Puts X if x!= "." && x!= "..."
}
Puts "Find a type of file"
dir["*.rb"].each {|x|
Puts X
}
Puts "Open query"
Dir.open (' C:/ruby ') {|d| d.grep/l/}.each{|x| puts X}
Puts "---------------------------"
Puts "Regular expression query"
dir["c:/ruby/ruby/[rs]*"].each{|x| puts X}
Puts "------------------------"
dir["c:/ruby/[^s]*"].each{|x| puts X}
Puts "------------------------"
dir["c:/ruby/{ruby,li}*"].each{|x| puts X}
Puts "------------------------"
dir["c:/ruby/?b*"].each{|x| puts X}
Puts "Find files for directories and subdirectories"
Require ' find '
Find.find ('./') {|path| puts path}

3, query directory and subdirectory files

Copy Code code as follows:

Require "find"
Find.find ("/etc/passwd", "/var/spool/lp1", ".") do |f|
Find.prune if f = = "."
Puts F
End

Prototype: Ref.find ([Aname]*) {| afilename | block}
Prune:skips the current file or directory and restarting the loop with the next entry. If the current file is a directory, that directory won't be recursively entered. Meaningful only within the blocks associated with Find::find.

4, file comparison copy, etc.

Copy Code code as follows:

Require ' ftools '
File.Copy ' testfile ', ' Testfile1 '»true
File.compare ' testfile ', ' Testfile1 '»true

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.