Ruby文件操作
2014-01-20 14:28
351 查看
一、新建文件
文件模式
"r":Read-only.Startsatbeginningoffile(defaultmode).
"r+":Read-write.Startsatbeginningoffile.
"w":Write-only.Truncatesexistingfiletozerolengthorcreatesanewfileforwriting.
"w+":Read-write.Truncatesexistingfiletozerolengthorcreatesanewfileforreadingandwriting.
"a":Write-only.Startsatendoffileiffileexists;otherwise,createsanewfileforwriting.
"a+":Read-write.Startsatendoffileiffileexists;otherwise,createsanewfileforreadingandwriting.
"b":(DOS/Windowsonly.)Binaryfilemode.Mayappearwithanyofthekeyletterslistedabove
二、读取文件
三、新建、删除、重命名文件
四、目录操作
1、ARGVandARGF
ARGV
ARGF
2、文件信息查询
3、查找文件
加载文件时需要:
require‘file’
但是加载这个文件前需要有个上下文环境,如:
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__),’..’,’file’)))
require如果返回true,说明成功读取了新的功能
加载路径,就是默认ruby启动的时候会在这些路径里去寻找可以加载的类库。ruby加载路径放在一个变量$LOAD_PATH($:)里。
函数解释:
File.dirname(__FILE__)得到当前文件的路径
File.join(‘x’,’y’,’z’)相当于x/y/z
File.expand_path('./x/y/z)组成一个绝对路径
$:.unshift("file")加入到$:变量中
支持通配符各正则表达式:
Dir.glob(Dir.glob(File.join(File.dirname(__FILE__),'../*.rb')).each{|f|requiref}
f= File . new ( File .join( "C:" , "Test.txt" ), "w+" ) |
f.puts( "IamJack" ) |
f.puts( "HelloWorld" ) |
"r":Read-only.Startsatbeginningoffile(defaultmode).
"r+":Read-write.Startsatbeginningoffile.
"w":Write-only.Truncatesexistingfiletozerolengthorcreatesanewfileforwriting.
"w+":Read-write.Truncatesexistingfiletozerolengthorcreatesanewfileforreadingandwriting.
"a":Write-only.Startsatendoffileiffileexists;otherwise,createsanewfileforwriting.
"a+":Read-write.Startsatendoffileiffileexists;otherwise,createsanewfileforreadingandwriting.
"b":(DOS/Windowsonly.)Binaryfilemode.Mayappearwithanyofthekeyletterslistedabove
二、读取文件
file= File .open( File .join( "C:" , "Test.txt" ), "r" ) |
file. [/b] {|line|print "#{file.lineno}." ,line } |
File . new ( "books.txt" , "w" ) |
File .rename( "books.txt" , "chaps.txt" ) |
File .delete( "chaps.txt" ) |
1 | 创建目录 |
Dir .mkdir( "c:/testdir" ) |
04 | #删除目录 |
05 | Dir .rmdir( "c:/testdir" ) |
07 | #查询目录里的文件 |
08 | p Dir .entries( File .join( "C:" , "Ruby[code])). join( ' ' |
10 | #遍历目录 |
11 | .entries( .join( "C:" , "Ruby[code])). each { |
|e|putse |
} |
ARGV
ARGV << "cnblogslink.txt" |
#ThegetsmethodisaKernelmethodthatgetslinesfromARGV |
while gets |
p ARGV . class |
while line= ARGF .gets |
printline |
end |
#文件是否存在 |
File : :exists ?( "cnblogslink.txt" # |
#是否是文件 |
p File .file?( "cnblogslink.txt" ) #=>true |
#是否是目录 |
p File : :directory ?( "c:/ruby" ) #=>true |
p File : :directory ?( "cnblogslink.txt" ) #=>false |
#文件权限 |
p File .readable[code]"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" ) #=>SatSep1908:05:07+08002009 |
p File : :mtime ( "cnblogslink.txt" ) #=>SatSep1908:06:34+08002009 |
p File : :atime ( "cnblogslink.txt" ) #=>SatSep1908:05:07+08002009 |
puts "查找目录下所有文件及文件夹" |
Dir [ "c:/ruby/*" ]. each {|x| |
putsx |
} |
puts "条件查询" |
Dir .foreach( 'c:/ruby' ){ |
|x|putsx if x!= "." &&x!= ".." |
} |
puts "查找某一类型文件" |
Dir [ "*.rb" ]. each {|x| |
putsx |
} |
puts "Open查询" |
Dir .open( 'c:/ruby' ){|d|d.grep/l/}. each {|x|putsx} |
puts "---------------------------" |
puts "正则表达式查询" |
Dir [ "c:/ruby/ruby/[rs]*" ]. each {|x|putsx} |
puts "------------------------" |
Dir [ "c:/ruby/[^s]*" ]. each {|x|putsx} |
puts "------------------------" |
Dir [ "c:/ruby/{ruby,li}*" ]. each {|x|putsx} |
puts "------------------------" |
Dir [ "c:/ruby/?b*" ]. each {|x|putsx} |
puts "查找目录及子目录的文件" |
require 'find' |
Find.find( './' ){|path|putspath} |
require‘file’
但是加载这个文件前需要有个上下文环境,如:
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__),’..’,’file’)))
require如果返回true,说明成功读取了新的功能
加载路径,就是默认ruby启动的时候会在这些路径里去寻找可以加载的类库。ruby加载路径放在一个变量$LOAD_PATH($:)里。
函数解释:
File.dirname(__FILE__)得到当前文件的路径
File.join(‘x’,’y’,’z’)相当于x/y/z
File.expand_path('./x/y/z)组成一个绝对路径
$:.unshift("file")加入到$:变量中
支持通配符各正则表达式:
Dir.glob(Dir.glob(File.join(File.dirname(__FILE__),'../*.rb')).each{|f|requiref}