Ruby文件操作

一、文件操作

1
2
f=File.new("#{File.dirname(__FILE__)}/temp.m", "w+")
f.puts("I am lmy")

  

1、文件模式

"r" :只读。从文件开头开始(默认模式)。
"r+" :读写。从文件的开头开始。
"w" :只写。将现有文件截断为零长度,或创建用于写入的新文件。
"w+" :读写。将现有文件截断为零长度,或创建用于读取和写入的新文件。
"a" :只写。如果文件存在,则在文件结尾处开始;否则,创建一个新文件以供编写。
"a+" :读写。如果文件存在,则在文件末尾开始;否则,创建用于读取和写入的新文件。
"b" :(仅限DOS/Windows)二进制文件模式。可能与上面列出的任何关键字母一起出现。

2、读取文件
1
2
3
file=File.open("#{File.dirname(__FILE__)}/temp.m","r")
file.each { |line| print "#{file.lineno}.", line }
file.close

  

上面读取文件,并一行行输出来。

3、新建、删除、重命名文件
1
2
3
File.new("#{File.dirname(__FILE__)}/tal.txt", "w" )
File.rename("#{File.dirname(__FILE__)}/tal.txt", "#{File.dirname(__FILE__)}/tal_test.txt")
File.delete("#{File.dirname(__FILE__)}/tal_test.txt")

  

二、目录操作

1、创建目录
1
Dir.mkdir("#{File.dirname(__FILE__)}/testDir")

  

2、删除目录
1
Dir.rmdir("#{File.dirname(__FILE__)}/testDir")

  

3、查询目录里的文件
1
2
p=Dir.entries("#{File.dirname(__FILE__)}/testDir")
puts(p)

  

 
4、遍历目录
1
2
3
Dir.entries("#{File.dirname(__FILE__)}/testDir").each {
          |e| puts e
    }

  

三、ARGV and ARGF

1、ARGV

在运行ruby脚本的时候,所有的参数会以Array的形式保存到ARGV中。

2、ARGF

ARGF则会根据ARGV中的值一个一个的处理,每处理一个就从ARGV中移除一个,直到处理完所有的值。

四、文件信息查询

文件是否存在

1
p=File::exists?( "cnblogslink.txt" ) # => true

  

是否是文件

1
p=File.file?( "cnblogslink.txt" ) # => true

  

是否是目录

1
2
p=File::directory?( "c:/ruby" ) # => true
p=File::directory?( "cnblogslink.txt" ) # => false

  

文件权限

1
2
3
p=File.readable?( "cnblogslink.txt" ) # => true
p=File.writable?( "cnblogslink.txt" ) # => true
p=File.executable?( "cnblogslink.txt" ) # => false

  

是否是零长度

1
p=File.zero?( "cnblogslink.txt" ) # => false

  

文件大小 bytes

1
2
p=File.size?( "cnblogslink.txt" ) # => 74
p=File.size( "cnblogslink.txt" ) # => 74

  

文件或文件夹

1
p=File::ftype( "cnblogslink.txt" ) # => "file"

  

文件创建、修改、最后一次存取时间

1
2
3
p=File::ctime( "cnblogslink.txt" ) # => Sat Sep 19 08:05:07 +0800 2019
p=File::mtime( "cnblogslink.txt" ) # => Sat Sep 19 08:06:34 +0800 2019
p=File::atime( "cnblogslink.txt" ) # => Sat Sep 19 08:05:07 +0800 2019

  

五、查找文件

1
2
3
puts "查找目录下所有文件及文件夹" <br>Dir["c:/ruby/*"].each {|x|
          puts x
    }

  

1
2
3
puts "条件查询"<br>Dir.foreach('c:/ruby') {
        |x| puts x if x != "." && x != ".."
    }

  

1
2
3
puts "查找某一类型文件"<br>Dir["*.rb"].each {|x|
      puts x
     }

  

1
2
3
4
5
6
7
8
9
10
puts "Open 查询" <br>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}  

 

1
2
puts "查找目录及子目录的文件" <br>require 'find'    
Find.find('./') { |path| puts path }

  

六、查询目录及子目录文件

1
2
3
4
5
6
7
  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.

  

七、文件比较 复制等

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

  






posted @   鞋带松了  阅读(210)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示