Ruby常用文件操作方法

所属分类: 脚本专栏 / ruby专题 阅读数: 648
收藏 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中任务构建工具rake的入门学习教程

这篇文章主要介绍了Ruby中任务构建工具rake的入门学习教程,讲解了包括命名空间和默认任务的执行等基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby程序中发送基于HTTP协议的请求的简单示例

这篇文章主要介绍了Ruby程序中发送基于HTTP协议的请求的简单示例,包括对HTTPS请求的介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby的字符串与数组求最大值的相关问题讨论

这篇文章主要介绍了Ruby中的字符串与数组求最大值的相关问题,文中还提到了sort排序方法的相关用法,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中Time对象的常用函数总结

这篇文章主要介绍了Ruby中Time对象的常用函数总结,包括Ruby中一些实用的时间算法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Ruby中正则表达式对字符串的匹配和替换操作

这篇文章主要介绍了Ruby中正则表达式对字符串的匹配和替换操作,包括对结果分组和一些特殊全局变量的介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

深入剖析Ruby设计模式编程中对命令模式的相关使用

这篇文章主要介绍了Ruby设计模式编程中对命令模式的相关使用,文中还讲到了关于观察者模式和命令模式的一些概念区别,需要的朋友可以参考下
收藏 0 赞 0 分享

实例解析Ruby设计模式开发中对观察者模式的实现

这篇文章主要介绍了实例解析Ruby设计模式开发中对观察者模式的实现,Ruby中自带的observer类自然是绝佳的使用示例,需要的朋友可以参考下
收藏 0 赞 0 分享

设计模式中的观察者模式在Ruby编程中的运用实例解析

这篇文章主要介绍了设计模式中的观察者模式在Ruby编程中的运用实例解析,观察者模式中主张设立观察者对象来降低对象之间的耦合,需要的朋友可以参考下
收藏 0 赞 0 分享

解析proxy代理模式在Ruby设计模式开发中的运用

这篇文章主要介绍了proxy代理模式在Ruby设计模式开发中的运用,通过代理模式以客户透明的形式可以动态地为目标对象加以行为控制,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多