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 existsotherwise, creates a new file for writing.
"a+" :Read-write. Starts at end of file if file existsotherwise, 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")
04 #删除目录
05 Dir.rmdir("c:/testdir")
07 #查询目录里的文件
08 p Dir.entries(File.join("C:","Ruby")).join(' ')
10 #遍历目录
11 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/?b*"].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
输入输出,我们平时简称做(I/O)的东西。想起做为脚本的javascript和vbscript不能使用标准输入输出我就不爽。也还记得刚开始学习java时,还没有Scanner这一说,就连读进一个整数都要很装B地StringTokenizer一番,实在是很苦的。好在Ruby很人性化,他不但实现了I/O,而且实现得还很不错,让我们一起来看一下吧。Ruby提供了两种不同的I/O方法。
第一种就是我们一直在用的RUBY的内核模块中的gets,open, print, printf, putc, puts, readline, readlines, 以及test。做为一直以来都在使用的standard input和standard output,我就不多做解释了。只要说一句大家就明白了,上面的函数如果出现在C中,那么实现就和C的语法一样,如果出现在JAVA中,你就直接使用JAVA的写法去调用,完全OK。
第二种方法就要体现出Ruby自己的东西了:I/O对象。接下来,我们就来看一下I/O对象的几类。还是从我们最常用的一个子类开始吧:File。
1、打开文件
可能你马上就想到了,既然File是一个类,那么new方法就应该可以用吧,呵呵,没错,我们可以用File.new(filename,openMode)这种方法来打开一个文件,相对的,我们也可以用File.close来关闭对一个文件的引用,这不需赘述。
可是我却要向大家推荐另外一种用法:File.open,这种方法也同样可以打开一个文件,那么他和new有何不同吗?在通常的应用之中,这两种方法并没有什么不同。但是如果在打开文件的方法之后附加了一个块的话,那他们的表现就不同了。Open方法将不再返回一个文件引用,而是去调用块里的代码,并把新打开的文件作为参数传给块使用。而当块中的代码执行完以后,文件将会自动关闭。Open的另外一个好处是,如果在执行过程中程序发生了异常,那么文件也将会自动关闭。而New就有可能因无法执行close而无法关闭文件。
2、读写文件
说起来没有什么大不了的,其实所谓读写文件不过是标准输入输出函数前面加上一个File.而已,也就是说我们用objFile.puts和objFile.gets来进行文件的读写就可以了。
3、使用迭代器读取文件
具体代码就是上面的那些
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)