Ruby常用文件操作方法

所属分类: 脚本专栏 / ruby专题 阅读数: 682
收藏 0 赞 0 分享

一、新建文件

复制代码 代码如下:

    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中用线程实现经典的生产者消费者问题代码实例,本文直接给出实现代码和运行效果,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中常用的字符串处理函数使用实例

这篇文章主要介绍了Ruby中常用的字符串处理函数使用实例,本文总结了Ruby中最常用的字符串处理函数,如返回字符串的长度、判断字符串中是否包含另一个串、字符串插入、字符串分隔、默认分隔符为空格等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows下ruby语言安装教程

这篇文章主要介绍了Windows下ruby语言安装教程,本文使用rubyinstaller提供的安装包安装,并给出图文说明,非常简单,需要的朋友可以参考下
收藏 0 赞 0 分享

ruby环境中自动编译sass教程

这篇文章主要介绍了ruby环境中自动编译sass教程,本文讲解了ruby环境的安装、sass环境的安装以及sass的常用编译命令使用示例,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中相等性判断的4种方法

这篇文章主要介绍了Ruby中相等性判断的4种方法,本文讲解了“==” 最常见的相等性判断、“===” 用于 case 语句的相容判断、“equal?” 相同对象判断、“eql?” 对象 hash 值判断等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Rails中使用MySQL分区表一个提升性能的方法

这篇文章主要介绍了Rails中使用MySQL分区表一个提升性能的方法,本文总结出了一个简单的方法实现避免扫描全部的分区表,从而提升性能,需要的朋友可以参考下
收藏 0 赞 0 分享

Rails应用程序中同时修改操作冲突问题的解决方案

这篇文章主要介绍了Rails应用程序中同时修改操作冲突问题的解决方案,本文讲解使用Rails 的 乐观锁解决这个问题并给出了代码救命,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中的p和puts的使用区别浅析

这篇文章主要介绍了Ruby中的p和puts的使用区别浅析,本文用一个实例讲解了它们之间的区别,并总结出结论,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中的block、proc、lambda区别总结

这篇文章主要介绍了Ruby中的block、proc、lambda区别总结,本文讲解了yield 和 block call 的区别、block 和 proc、lambda 的区别、proc 和 lambda 的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby的运算符和语句优先级介绍

这篇文章主要介绍了Ruby的运算符和语句优先级介绍,本文先是给出了一些小例子来验证运算符和语句优先级,然后总结出一个优先级表,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多