Common file operation methods in Ruby

Time:2022-5-3

1、 New 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

 

2、 Read file

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 files

Copy codeThe code is as follows:

    File.new( “books.txt”, “w” )
    File.rename( “books.txt”, “chaps.txt” )
    File.delete( “chaps.txt” )


4、 Directory operation
1. Create directory

Copy codeThe code is as follows:

    Dir.mkdir(“c:/testdir”)
# delete directory
     Dir.rmdir(“c:/testdir”)
# query the files in the directory
     p Dir.entries(File.join(“C:”,”Ruby”)).join(‘ ‘)
# traverse directory
     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:

# 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 permissions
    p File.readable?( “cnblogslink.txt” ) # => true
    p File.writable?( “cnblogslink.txt” ) # => true
    p File.executable?( “cnblogslink.txt” ) # => false
# is it 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 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. Find file

Copy codeThe code is as follows:

Put “find all files and folders in the directory”
    Dir[“c:/ruby/*”].each {|x|
          puts x
    }
Put “condition query”
    Dir.foreach(‘c:/ruby’) {
        |x| puts x if x != “.” && x != “..”
    }
Put “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}       
Put “find files in directories and subdirectories”
    require ‘find’    
    Find.find(‘./’) { |path| puts path }

 

3. Query directory 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 ([ananame] *) {| 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

Copy codeThe code is as follows:

    require ‘ftools’
    File.copy ‘testfile’, ‘testfile1’  » true
    File.compare ‘testfile’, ‘testfile1’  » true

Recommended Today

React explanation – parent component calls child component content [updating]

preface This article belongs to react communication > parent-child communication > parent component calling child component.Scenario of parent component calling child component: Sub components are used in multiple places and need to be encapsulated separately The logic of sub components is heavy, and the cost of using fully controlled mode is high Using parent components […]