您的位置:首页 > 其它

watir学习系列--文件操作(创建文件)

2008-08-12 09:41 302 查看
class CreateFile #定义类,类名首字母需大写

def rows_to_file(rows) #定义类方法

@@data_rows = rows #定义类变量,无需申明变量类型

@@content = ''

@@filename = "c:\\filename.txt" #定义文件路径

@@data_rows.each do |x| #遍历数组

@@content = @@content + x.to_s + ‘-‘ #to_s:转换为字符串

end

#写文件,参数说明:"a+" 在原有内容后追加数据 "w" :先将文件清空,再写入

File.open( @@filename, "a+" ) do |file|

file.puts @@content

end

end

end

rows = ['a','b','c']

CreateFile = CreateFile.new

CreateFile.rows_to_file(rows)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: