• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby常用文件操作方法

Ruby常用文件操作方法

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了ruby 文件操作,ruby操作excel,ruby 读取文件,ruby读文件,如何运行ruby文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、新建文件
    f=File.new(File.join("C:","Test.txt"), "w+")
    f.puts("I am Jack")
    f.puts("Hello World")
</div>
文件模式
"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
</div>
三、新建、删除、重命名文件
    File.new( "books.txt", "w" )
    File.rename( "books.txt", "chaps.txt" )
    File.delete( "chaps.txt" )
</div>
四、目录操作
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
    }
</div>
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
</div>
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
</div>
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 }
</div>

3、查询目录及子目录文件
    require "find"
Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|
  Find.prune if f == "."
  puts f
end
</div>
原型: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、文件比较 复制等

您可能想查找下面的文章:

  • ruby 去掉文件里重复的行
  • 在操作系统上安装Ruby解释器的教程
  • Ruby常用文件操作方法
  • Ruby中操作文件的方法介绍
  • Ruby中处理时间的一些基本操作
  • 在Ruby中处理文件的输入和输出的教程
  • Ruby中一些常用的文件操作方法小结
  • Ruby实现批量对文件增加前缀代码分享
  • ruby实现的文件自删除代码分享

相关文章

  • Ruby基本的环境变量设置以及常用解释器命令介绍
  • 设计模式中的观察者模式在Ruby编程中的运用实例解析
  • 更改RubyGem安装源
  • Ruby实现批量对文件增加前缀代码分享
  • Ruby on Rails中的ActiveRecord编程指南
  • 对Ruby on Rails进行高效的单元测试的教程
  • ruby实现网页图片抓取
  • 详解Ruby中的块的知识
  • Ruby 中关于日文转UTF-8及半角全角转换的技巧
  • 详细解读Ruby当中的条件判断语句

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • ruby基本数据类型简明介绍
    • ruby 正则表达式详解及示例代码
    • Ruby简洁学习笔记(一):字符串、数字、类和对象
    • Ruby中处理时间的一些基本操作
    • 介绍Ruby中的模块与混合类型的相关知识
    • 浅析Ruby的源代码布局及其编程风格
    • 使用Ruby re模块创建复杂的正则表达式
    • win7安装ruby on rails开发环境
    • ruby实现网页图片抓取
    • GitHub倡导的Ruby代码编写风格总结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有