This article mainly introduces common file operation methods in Ruby.This article explains common file operation methods such as creating new files, reading files, deleting, renaming files, and directory operations. Friends in need can refer to the following
I. New file
code show as below:
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 with any of the key letters listed above
Second, read the file
code show as below:
File = File.open (File.join ("C:", "Test.txt"), "r")
File.each {| line | print "# {file.lineno}.", Line}
File.close
Third, create, delete, rename files
code show as below:
File.new ("books.txt", "w")
File.rename ("books.txt", "chaps.txt")
File.delete ("chaps.txt")
Fourth, directory operation
1 Create directory
Copy code The code is as follows:
Dir.mkdir ("c: / testdir")
# Delete directory
Dir.rmdir ("c: / testdir")
#Query files in the directory
P Dir.entries (File.join ("C:", "Ruby")). Join ('')
# Traversing the directory
Dir.entries (File.join ("C:", "Ruby")). Each {
| E | puts e
}
1, ARGV and ARGF
code show as below:
ARGV
ARGV << "cnblogslink.txt"
#The gets method is a Kernel method that gets lines from ARGV
Print while gets
P ARGV.class
ARGF
While line = ARGF.gets
Print line
End
2, file information query
code show as below:
# File exists
P File :: exists? ("Cnblogslink.txt") # => true
# Is it a file
P File.file? ("Cnblogslink.txt") # => true
# Is it a directory
P File :: directory? ("C: / ruby") # => true
P File :: directory? ("Cnblogslink.txt") # => false
#file permission
P File.readable? ("Cnblogslink.txt") # => true
P File.writable? ("Cnblogslink.txt") # => true
P File.executable? ("Cnblogslink.txt") # => false
# Is zero length
P File.zero? ("Cnblogslink.txt") # => false
#File size bytes
P File.size? ("Cnblogslink.txt") # => 74
P File.size ("cnblogslink.txt") # => 74
#File or folder
P File :: ftype ("cnblogslink.txt") # => "file"
#File creation, 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
code show as below:
Puts "Find all files and folders in a directory"
Dir ["c: / ruby / *"]. Each {| x |
Puts x
}
Puts "Condition query"
Dir.foreach ('c: / ruby') {
| X | puts x if x! = "." && x! = ".."
}
Puts "Find a certain 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 in directories and subdirectories"
Require 'find'
Find.find ('./') {| path | puts path}
3. Query directory and subdirectory files
code show as below:
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, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find :: find.
4, file comparison, copying, etc.
code show as below:
Require 'ftools'
File.copy 'testfile', 'testfile1' »true
File.compare 'testfile', 'testfile1' »true