Chinaunix

标题: ruby文件操作 [打印本页]

作者: 2gua    时间: 2011-06-26 07:16
标题: ruby文件操作
1、文件的打开与关闭
    ``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.new方法获取一个文件句柄来对文件操作,操作结束后file.close来关闭文件。
     file.open方法是new方法的扩充,该方法可有代码块,该代码块结束后自动close,而且在操作过程中发生错误时能够自动收集错误并推出
    如   
    file.open("filepath") do |file|
        file.each do |line| ... end
    end
    一些文件的常用命令:
    File.open(dir+"/read.txt","w") do |file|
    file.puts("djkjsadlkjdkdsfdsee")
end

puts File.exists?(dir+"/read.txt")       文件是否存在
puts File.directory?(dir+"/read.txt")    文件是否是目录路径
puts File.file?(dir+"/read.txt")        是否是文件
puts File.zero?(dir+"/read.txt")        文件内容长度是否为0
puts File.size(dir+"/read.txt")        获取文件大小
puts File.readable?(dir+"/read.txt")    文件是否可读
puts File.stat(dir+"/read.txt")        文件状态,文件实例
puts File.basename(dir+"/read.txt",".txt")    文件名称

f = File.new(dir+"/read.txt","r")
puts  File.stat(dir+"/read.txt").atime
puts f.atime
puts f.path
#f.delete #删除文件
f.close

2 目录文件操作
    Dir.foreach(dir) do |ff|
    puts ff
end
输出:
.
..
fileOpt.rb
read.txt
test.rb
tt
write.txt

Dir[dir+"/*"] .each do |ff|   #这种方法可在*出增加条件如*.rb来指定文件类型,或aa*.rb甚至可以使用正则表达式
    puts ff
end
输出:
E:/WorkSpace/ruby/fileOpt.rb
E:/WorkSpace/ruby/read.txt
E:/WorkSpace/ruby/test.rb
E:/WorkSpace/ruby/tt
E:/WorkSpace/ruby/write.txt

#~ Dir.mkdir(dir+"/tt")  创建路径
Dir.rmdir(dir+"/tt")   路径删除
p Dir.entries(dir)   
输出:
[".", "..", "fileOpt.rb", "read.txt", "test.rb", "write.txt"]

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
作者: 2gua    时间: 2011-06-26 07:17
有人问,就发个。
作者: 2gua    时间: 2011-06-26 07:17
老黄瓜,自己看哦。
作者: rubyish    时间: 2011-07-20 16:54
看看哦。
作者: 2gua    时间: 2011-07-20 20:28
老黄瓜呢?咋不来看哦?
作者: 2gua    时间: 2011-07-20 20:28
回复 4# rubyish


    你也来发啊。
作者: shijiang1130    时间: 2011-07-21 16:02
ruby -n -e "print if /Ruby/" test.txt
作者: 2gua    时间: 2011-07-21 22:45
楼上想说啥?
作者: i_love_ruby    时间: 2011-09-16 17:43
学习了.
谢谢楼主!!!!!!!!!!!!!
作者: gr33n    时间: 2011-11-21 17:10
学习了.谢谢楼主!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2