欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

ruby base之文件操作

data =File.stat(__FILE__)
puts data.mode
File.chmod(777,__FILE__) #change the permission of file
data = File.stat(__FILE__)
puts data.mode
puts data.mtime  #last read
puts data.atime  #last update
puts data.ctime  #last create
file = File.open(“F:/self/mycode/FileTest.rb”,”a+”)
begin
#file.close_write
puts s
rescue => e
puts e
puts $!
end
puts file.read
#f = File.new(“F:/self/mycode/aa.txt”,”a”)
#f.write(“2012-2-14dd”)
#f.close
#f = File.new(“F:/self/mycode/aa.txt”)
#puts f.read
#puts FileTest.size(“F:/self/mycode/aa.txt”).to_s+”kb”
puts “size of file:”+file.size.to_s+”kb”

#移动指针 查找
puts file.pos
p file.read(2)
file.rewind
p file.read(2)
p file.lineno
p file.gets
file.rewind
p file.readline
puts “readlines:”
p file.rewind
p file.readlines
file.rewind
file.each do |f|
puts f
end
#使用IO类的方法迭代内容文件\
puts “use IO”
IO.foreach “F:/self/mycode/FileTest.rb” do |line|
puts line
end
file.puts(“ok”)
file.write(“write”)
file.print(“here is the print method”)
file.print(“do you know”)
file.print(“i use the $\to change line”)
file.rewind
puts file.read

Tags:

发表评论