Ruby常用文件操作方法
程序员文章站
2022-04-28 22:20:16
一、新建文件
复制代码 代码如下:
f=file.new(file.join("c:","test.txt"), "w+")
&...
一、新建文件
复制代码 代码如下:
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