Common File Operations in Ruby and ruby
1. Create a file
Copy codeThe Code is 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 with any of the key letters listed above
Ii. Reading files
Copy codeThe Code is as follows:
File = File. open (File. join ("C:", "Test.txt"), "r ")
File. each {| line | print "# {file. lineno}.", line}
File. close
3. Create, delete, and rename an object
Copy codeThe Code is as follows:
File. new ("books.txt", "w ")
File. rename ("books.txt", "chaps.txt ")
File. delete ("chaps.txt ")
Iv. Directory operations
1. Create a directory
Copy codeThe Code is as follows:
Dir. mkdir ("c:/testdir ")
# Deleting a directory
Dir. rmdir ("c:/testdir ")
# Querying files in a directory
P Dir. entries (File. join ("C:", "Ruby"). join ('')
# Traverse directories
Dir. entries (File. join ("C:", "Ruby"). each {
| E | puts e
}
1. ARGV and ARGF
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
# Whether the file exists
P File: exists? ("Cnblogslink.txt") # => true
# Whether it is a file
P File. file? ("Cnblogslink.txt") # => true
# Whether it is 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.exe cutable? ("Cnblogslink.txt") # => false
# Whether it is zero-length
P File. zero? ("Cnblogslink.txt") # => false
# File size bytes
P File. size? ("Cnblogslink.txt") # => 74
P File. size ("cnblogslink.txt") # => 74
# Files or folders
P File: ftype ("cnblogslink.txt") # => "file"
# File creation, modification, and 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. Search for files
Copy codeThe Code is as follows:
Puts "Find all files and folders in the directory"
Dir ["c:/ruby/*"]. each {| x |
Puts x
}
Puts "condition 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. grep/l/}. each {| x | puts x}
Puts "---------------------------"
Puts "Regular Expression query"
Dir ["c:/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 directories and subdirectory Files"
Require 'Find'
Find. find ('./') {| path | puts path}
3. query directories and subdirectory files
Copy codeThe Code is 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, 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 and Replication
Copy codeThe Code is as follows:
Require 'ftool'
File. copy 'testfile', 'testfile1'» true
File. compare 'testfile', 'testfile1'» true