file1 = File.new("one") # Open for reading file2 = File.new("two", "w") # Open for writing另外一种new的形式是三个参数的,其中第二个参数是指定了这个文件的原始的权限(经常表示为一个八进制的数).第三个参数是一系列Ored标志 的组合.标志是个常量比如File:CREAT(如果文件不存在则创建它)和File:RDONLY(以只读方式打开文件)。不过这种形式很少使用:
file = File.new("three", 0755, File::CREAT|File::WRONLY)
out = File.new("captains.log", "w") # Process as needed... out.close
trans = File.open("transactions","w")
File.open("somefile","w") do |file| file.puts "Line 1" file.puts "Line 2" file.puts "Third and final line" end
f1 = File.new("file1", "r+") # Read/write, starting at beginning of file. f2 = File.new("file2", "w+") # Read/write; truncate existing file or create a new one. f3 = File.new("file3", "a+") # Read/write; start at end of existing file or create a # new one.
logfile = File.open("captains_log", "a") # Add a line at the end, then close. logfile.puts "Stardate 47824.1: Our show has been canceled." logfile.close
# myfile contains only: abcdefghi file = File.new("myfile") file.seek(5) str = file.gets # "fghi"
# Assume 20 bytes per line. # Line N starts at byte (N-1)*20 file = File.new("fixedlines") file.seek(5*20) # Sixth line! # Elegance is left as an exercise.如果你想做一个相对的搜索,你就要使用第二个参数,常量 IO::SEEK_CUR表示当前的位置,而第一个参数则就是相对于当前位置的偏移量(可能是负数):
file = File.new("somefile") file.seek(55) # Position is 55 file.seek(-22, IO::SEEK_CUR) # Position is 33 file.seek(47, IO::SEEK_CUR) # Position is 80
file.seek(-20, IO::SEEK_END) # twenty bytes from eof方法tell得到文件的当前位置,pos是它的别名:
file.seek(20) pos1 = file.tell # 20 file.seek(50, IO::SEEK_CUR) pos2 = file.pos # 70rewind方法将会将文件指针的位置设回到开始的位置,也就是0.
# Create a file (in binary mode) File.open("myfile","wb") {|f| f.syswrite("12345\0326789\r") } # Above note the embedded octal 032 (^Z) # Read it as binary str = nil File.open("myfile","rb") {|f| str = f.sysread(15) } puts str.size # 11 # Read it as text str = nil File.open("myfile","r") {|f| str = f.sysread(15) } puts str.size # 5这边注意,这些代码都是在windows下才会打印出后面的结果,如果是在linux两处都会打印出11.
# Input file contains a single line: Line 1. file = File.open("data") line = file.readline # "Line 1.\n" puts "#{line.size} characters." # 8 characters file.close file = File.open("data","rb") line = file.readline # "Line 1.\r\n" puts "#{line.size} characters." # 9 characters 二进制模式的结尾是一个回车换行对. file.closebinmode方法能够转换当前的流为二进制模式,这边要注意的是,一旦切换过去,就不能切换回来了:
file = File.open("data") file.binmode line = file.readline # "Line 1.\r\n" puts "#{line.size} characters." # 9 characters file.close如果你想使用更底层的输入输出,那你可以选择sysread和syswrite方法,他们接受一定数量的字节作为参数 .
input = File.new("myfile",'a+') output = File.new("outfile",'a+') instr = input.sysread(10); puts instr bytes = output.syswrite("This is a test.")
如果文件指针已经到达文件的结尾时,sysread方法将会抛出一个异常.
这边要注意 Array 的pack和string的unpack方法,对于处理二进制数据非常有用.
=======================================================
读文件:
第一种方法:
$result='d:\\rs.txt'
File.open($result, "r") do |file|
file.each_line do |line|
if line.length>20
puts line.chop.length #去掉最后一个换行字符,并显示该行实际字符串的长度
puts line
end
end
end
第二种方法:
filename='d:\\rs.txt'
while File.exists?(filename) #如果源文件存在就执行下面的操作
file=File.open(filename,'r')
while (lines=file.gets)
puts lines
end
写文件:
$filename="C:\\Automation\\rss"+".txt"
$logfile = File.new($filename,"a")
iCount=0
while(iCount<10) //循环写入10行
$logfile.puts "http://xxxx/rs#{iCount}.xml"
iCount=iCount+1
end
今天又笨了一次,由于要比较两个文件的不同,于是考虑用ruby来写个脚本
实现,刚开始的时候使用双重
File.open($file1, "r") do |file1|
file1.each_line do |line1|
总是报错,
后来改成先把文件读到一个数组里,然后循环比较,成功.
其实这是个笨方法,在unix下使用三个命令就可以完成了.
1.先使用sort指令将文件中的数据按照要求的索引进行排序,
2.然后使用uniq指令将重复数据去掉
3.再使用diff命令就可以了.
========================================
ruby读写文本文件的简单例子,除了演示文本文件的读写外,这段ruby程序可以从文本文件中删除包含某些字符串的行。
用法:ruby delline.rb 文件名 字符串1 字符串2 字符串n
将删除同时包含字符串1 字符串2 字符串n的行。
ruby的开发环境这里下载
http://www.ruby-lang.org/en/downloads/
直接下载最近的稳定版Windows安装包
http://rubyforge.org/frs/download.php/29263/ruby186-26.exe
代码如下
File.open("cmd.txt","r") do |file|
while line=file.gets
puts line
end
end
puts
file=File.new("cmd.txt","r")
file.each_line do |line|
puts line
end
IO.foreach("cmd.txt") do |line|
puts line if line =~ /target/
puts line if line !~ /target/
end
# Another way...
file = File.new("cmd.txt")
file.each do |line|
puts line if line =~ /target/
end
arr = IO.read("cmd.txt")
bytes = arr.size
puts "myfile has #{bytes} bytes in it."
arr = IO.readlines("cmd.txt")
puts arr
lines = arr.size
puts "myfile has #{lines} lines in it."
file2=File.new("cmd.txt");
puts File.expand_path("cmd.txt")
file = File.new("cmd.txt")
e_count = 0
file.each_byte do |byte|
e_count += 1 if byte == ?e
end
puts File.exist?("testR.txt")
file1=File.new("testR.txt","w") #没有文件则会自动创建
puts File.exist?("testR.txt")
puts file1.write("ttt333\nttt444\n")
字符串当文件用.rb
require 'stringio'
ios = StringIO.new("abcdefghijkl\nABC\n123")
ios.seek(5)
ios.puts("xyz")
puts ios.tell # 8
puts ios.string.dump # "abcdexyzijkl\nABC\n123"
c = ios.getc
puts "c = #{c}" # c = 105
ios.ungetc(?w)
puts ios.string.dump # "abcdexyzwjkl\nABC\n123"
puts "Ptr = #{ios.tell}"
s1 = ios.gets # "wjkl"
s2 = ios.gets # "ABC"