一、新建文件
复制代码 代码如下:
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/"].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
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《阿瑞亚之旅》评测:更适合佛系玩家体质的跑团
- 黑神话悟空浮屠界紫门怎么开 浮屠界紫门打开方法介绍
- 黑神话悟空龟泪怎么获得 黑神话悟空龟泪获得方法介绍
- 黑神话悟空怎么获得还魂丹 黑神话悟空还魂丹获取方法一览
- HiFi顶级人声天碟《试音齐秦》wav
- 张敬轩.2012-WhyNot(EP)【环球】【WAV+CUE】
- 张小英1988-情愁[新加坡复黑版][WAV+CUE]
- 游民金币礼品补货啦 Steam充值卡等你兑换!
- 《暗喻幻想:ReFantazio》M站均分94分 近年最好的RPG游戏之一
- 今年第一项诺贝尔奖揭幕 生理学或医学奖揭晓
- 《梁静茹 华语乐坛情歌天后 爱久见人心》[WAV/分轨][360MB]
- 《田震 绝版珍藏 依然在飞》[WAV+CUE][600MB]
- 《60首经典金曲 4CD》[WAV/分轨][1.4GB]
- 赵传.1988-我很丑,可是我很温柔【滚石】【WAV+CUE】
- 遗忘曲线.2024-遗忘曲线(EP)【LuuvLabel】【FLAC分轨】