免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7650 | 回复: 9
打印 上一主题 下一主题

ruby文件操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-26 07:16 |只看该作者 |倒序浏览
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

论坛徽章:
0
2 [报告]
发表于 2011-06-26 07:17 |只看该作者
有人问,就发个。

论坛徽章:
0
3 [报告]
发表于 2011-06-26 07:17 |只看该作者
老黄瓜,自己看哦。

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
4 [报告]
发表于 2011-07-20 16:54 |只看该作者
看看哦。

论坛徽章:
0
5 [报告]
发表于 2011-07-20 20:28 |只看该作者
老黄瓜呢?咋不来看哦?

论坛徽章:
0
6 [报告]
发表于 2011-07-20 20:28 |只看该作者
回复 4# rubyish


    你也来发啊。

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
7 [报告]
发表于 2011-07-21 16:02 |只看该作者
ruby -n -e "print if /Ruby/" test.txt

论坛徽章:
0
8 [报告]
发表于 2011-07-21 22:45 |只看该作者
楼上想说啥?

论坛徽章:
0
9 [报告]
发表于 2011-09-16 17:43 |只看该作者
学习了.
谢谢楼主!!!!!!!!!!!!!

论坛徽章:
0
10 [报告]
发表于 2011-11-21 17:10 |只看该作者
学习了.谢谢楼主!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP