好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Ruby常用文件操作方法

Ruby常用文件操作方法

熱門標簽:常德電銷平臺外呼系統軟件價格 百度地圖標注自定義圖片 電銷機器人廠商代理 高德地圖標注客服 徐州網絡外呼系統哪個好 滴滴外呼系統 白銀外呼paas系統 湖州u友防封電銷卡 地圖標注賺錢項目注冊

一、新建文件

復制代碼 代碼如下:

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

文件模式
"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

二、讀取文件

復制代碼 代碼如下:

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

三、新建、刪除、重命名文件
復制代碼 代碼如下:

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

四、目錄操作
1     創建目錄
復制代碼 代碼如下:

    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
復制代碼 代碼如下:

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、文件信息查詢
復制代碼 代碼如下:

    #文件是否存在
    p File::exists?( "cnblogslink.txt" ) # => true
    #是否是文件
    p File.file?( "cnblogslink.txt" ) # => true
    #是否是目錄
    p File::directory?( "c:/ruby" ) # => true
    p File::directory?( "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"
    #文件創建、修改、最后一次存取時間
    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、查找文件
復制代碼 代碼如下:

    puts "查找目錄下所有文件及文件夾"
    Dir["c:/ruby/*"].each {|x|
          puts x
    }
    puts "條件查詢"
    Dir.foreach('c:/ruby') {
        |x| puts x if x != "." x != ".."
    }
    puts "查找某一類型文件"
    Dir["*.rb"].each {|x|
      puts x
     }
    puts "Open 查詢"
    Dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}
    puts "---------------------------"     
    puts "正則表達式查詢"
    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 "查找目錄及子目錄的文件"
    require 'find'    
    Find.find('./') { |path| puts path }

3、查詢目錄及子目錄文件

復制代碼 代碼如下:

    require "find"
Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|
  Find.prune if f == "."
  puts f
end

原型: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、文件比較 復制等

復制代碼 代碼如下:

    require 'ftools'
    File.copy 'testfile', 'testfile1'  » true
    File.compare 'testfile', 'testfile1'  » true

標簽:遼寧 公主嶺 荊門 張家界 三沙 梧州 普洱 永州

巨人網絡通訊聲明:本文標題《Ruby常用文件操作方法》,本文關鍵詞  Ruby,常用,文件,操作,方法,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Ruby常用文件操作方法》相關的同類信息!
  • 本頁收集關于Ruby常用文件操作方法的相關信息資訊供網民參考!
  • 推薦文章