RUBY文件读写

来源:互联网 发布:centos虚拟机连接网络 编辑:程序博客网 时间:2024/06/05 10:46
 标准输入流:gets 读文件 

              File.open("hello.rb","r") do |file|

               while line  = file.gets

                   puts line #打印出文件内容

               end

             end

    标准输出流:puts
    print
    两者的区别是puts会在参数后面添加回车换行,print不会添加
    printf("Number:%5.2f,\nString:%s\n",1.23,"hello") 这个语法跟c相同就不多说了
    %5.2f  匹配  1.23

    %s  匹配字符串

[plain] view plaincopyprint?
  1. 1.先写段代码看看  
  2.     #p1  
  3.     myFile = File.new("f:\\ruby\\mycode\\hello.rb","w");  
  4.     myFile.puts "puts 'aa'"  
  5.     myFile.puts "puts 'bb'"  
  6.     myFile.close #只有close掉了内容才被写入文件里面。  
  7.     windows中路径 "\\"  
  8.       
  9.     文件hello.rb写入上面两行代码  
  10.     创建文件:File.new("hello.rb","w")  
  11.     删除文件:File.delete("")  
  12.     读取文件:File.open("hello.rb","r") do |file|  
  13.                 while line = file.gets   #标准输入流  
  14.                    puts line  
  15.                 end  
  16.               end  
  17.     #读文件  
  18.     print "Please input a file name:"  
  19.     filename = gets  
  20.     if filename &&!filename.empty?#文件存在  
  21.         filename = filename[0,filename.length-1]  
  22.         #去掉文件名后面的"\n"  
  23.     else  
  24.         print "the file name can't be null!"  
  25.         exit(1)  
  26.     end  
  27.       
  28.     if File.exist?(filename)  
  29.         puts "=========#{filename}========="  
  30.         File.open(filename,"r") do |file|  
  31.              while line = file.gets  
  32.                 puts line  
  33.              end  
  34.         end  
  35.         puts "=================="  
  36.     else  
  37.         puts "the program can't find the file #{filename}"  
  38.     end  
  39.     print "Press any key to contiue..."  
  40.     gets  
  41.       
  42.       
  43.     #写文件  
  44.     puts "======================================"  
  45.     puts "This program is about Ruby write file."  
  46.     puts "======================================"  
  47.     print "Please input file name: "  
  48.     filename=gets  
  49.     if filename&&!filename.empty?  
  50.         filename=filename[0, filename.length-1]  
  51.     else  
  52.         puts "The file name can't been null!!"  
  53.         exit 1;  
  54.     end  
  55.     file=nil  
  56.     unless File.exist?(filename)#条件不成立的时候执行  
  57.        puts "The system cannot find the file specified!"  
  58.        print "[C] to create a new file and [E] to exit the program: "  
  59.        option=gets  
  60.        if option&&!option.empty?  
  61.           option=option.chomp#去掉"\n"  
  62.        else  
  63.           puts "bye!"  
  64.           exit 1;  
  65.        end  
  66.        case option.downcase  
  67.        when "c" : file=File.new(filename, "w")  
  68.        when "e" : exit 0  
  69.        else  
  70.           puts "Invalid arguments!! The program has stop. "  
  71.           exit 0  
  72.        end  
  73.     else  
  74.         file=File.new(filename, "w")  
  75.     end  
  76.     print "Now please input content: "  
  77.     content=gets  
  78.     file.print content  
  79.     file.close  
  80.     print "Press any key to continue...."  
  81.     gets  

原创粉丝点击