RUBY文件读写
来源:互联网 发布:学珠宝设计软件 编辑:程序博客网 时间:2024/06/06 03:50
11.读写
标准输入流:gets 读文件
File.open("hello.rb","r") do |file|
while line = file.gets
puts line #打印出文件内容
end
end
两者的区别是puts会在参数后面添加回车换行,print不会添加
printf("Number:%5.2f,\nString:%s\n",1.23,"hello") 这个语法跟c相同就不多说了
%5.2f 匹配 1.23
%s 匹配字符串
1.先写段代码看看 #p1 myFile = File.new("f:\\ruby\\mycode\\hello.rb","w"); myFile.puts "puts 'aa'" myFile.puts "puts 'bb'" myFile.close #只有close掉了内容才被写入文件里面。 windows中路径 "\\" 文件hello.rb写入上面两行代码 创建文件:File.new("hello.rb","w") 删除文件:File.delete("") 读取文件:File.open("hello.rb","r") do |file| while line = file.gets #标准输入流 puts line end end #读文件 print "Please input a file name:" filename = gets if filename &&!filename.empty?#文件存在 filename = filename[0,filename.length-1] #去掉文件名后面的"\n" else print "the file name can't be null!" exit(1) end if File.exist?(filename) puts "=========#{filename}=========" File.open(filename,"r") do |file| while line = file.gets puts line end end puts "==================" else puts "the program can't find the file #{filename}" end print "Press any key to contiue..." gets #写文件 puts "======================================" puts "This program is about Ruby write file." puts "======================================" print "Please input file name: " filename=gets if filename&&!filename.empty? filename=filename[0, filename.length-1] else puts "The file name can't been null!!" exit 1; end file=nil unless File.exist?(filename)#条件不成立的时候执行 puts "The system cannot find the file specified!" print "[C] to create a new file and [E] to exit the program: " option=gets if option&&!option.empty? option=option.chomp#去掉"\n" else puts "bye!" exit 1; end case option.downcase when "c" : file=File.new(filename, "w") when "e" : exit 0 else puts "Invalid arguments!! The program has stop. " exit 0 end else file=File.new(filename, "w") end print "Now please input content: " content=gets file.print content file.close print "Press any key to continue...." gets
- [Ruby]读写excel文件
- Ruby 读写文件
- RUBY文件读写
- RUBY文件读写
- ruby 文件读写操作
- ruby读写txt文件
- ruby: 读写excel文件
- ruby: 读写excel文件
- RUBY文件读写
- ruby 读写文件
- RUBY文件读写
- MetaSploit常用命令 ruby读写文件
- Ruby简单的读写文件
- Ruby简单的读写文件
- 使用Ruby写xml文件 , Ruby xml , Ruby读写xml文件 ,Ruby创建xml文件
- ruby: 嵌入字符串、% 呈现、ruby文件读写ruby way之IO之一
- ruby中读写文件后中文字符串的处理
- ruby文件读写的好文章 ruby way之IO之一
- Nutch中MapReduce的分析
- Intent Flag参数
- Php的mysqli扩展
- 下午试题第二道题:反规范化技术
- vmware网络设置,nat上网方式
- RUBY文件读写
- Nutch 插件系统浅析
- 什么是MD5算法?
- shell脚本 if参数
- [转]这样学英语三个月超过你过去学三年
- Yii CDbCriteria常用用法
- SQL Server 2008出现“阻止保存要求重新创建表的更改”错误
- Eclipse CDT 解决 "for loop initial declaration used outside C99 mode" 错误
- win7 系统 Silverlight 视频个性化组件 安装失败。。解决方案