使用Ruby读取文件会返回奇怪的输出

时间:2012-10-04 18:11:21

标签: ruby

我正在尝试使用JSON读取Ruby文件,输出非常奇怪。这是我正在使用的代码:

require 'rubygems'

class ServiceCalls 

    def initialize ()

    end

    def getFile()

        Dir.entries('./json').each do |mFile|

            if mFile[0,1] != "."
                self.sendServiceRequest(mFile)
            end

        end
    end

    def sendServiceRequest(mFile)
        currentFile = File.new("./json/" + mFile, "r") 
        puts currentFile.read
        currentFile.close
    end



end

mServiceCalls = ServiceCalls.new
mServiceCalls.getFile

这是输出:

Macintosh H??=A?v?P$66267945-2481-3907-B88A-1094AA9DAB6D??/??is32???????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz???????????????????????????????????????????????????????????s8m+88888888???????89????????99?????????9:??????????:;??????????;=??????????=>??????????>????????????@??????????@A??????????AC??????????CD??????????DE??????????EE??????????E6OXdknnkdXO6ic118?PNG
bookmark88?A[DT>??A?@
                     ApplicationsMAMPhtdocsServiceTestAutomationMDXservicecatalog-verizon.json$4T??
                  `?
                   U?????l??????
                                Macintosh H??=A?v?P$66267945-2481-3907-B88A-1094?is32???????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz???????????????????????????????????????????????????????????s8m+88888888???????89????????99?????????9:??????????:;??????????;=??????????=>??????????>????????????@??????????@A??????????AC??????????CD??????????DE??????????EE??????????E6OXdknnkdXO6ic118?PNG
UIEvolutions-MacBook-Pro-109:MDXServiceTesting Banderson$ ruby testmdxservices.rb
bookmark88?A?,P>??A?@
                     ApplicationsMAMPhtdocsServiceTestAutomationMDXservicecatalog-adaptation.json$4T??
                     `?
                      U?????l??????
                                   Macintosh H??=A?v?P$66267945-2481-3907-B88A-1094AA9DAB6D??/?<icns<?TOC his32?s8mic118il32?l8mic1?ic07ic13#ic08#ic14^?ic09_ic1?is32???????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz???????????????????????????????????????????????????????????s8m+88888888???????89????????99?????????9:??????????:;??????????;=??????????=>??????????>????????????@??????????@A??????????AC??????????CD??????????DE??????????EE??????????E6OXdknnkdXO6ic118?PNG

IHDR szz?iCCPICC Profile(?T?k?P??e???:g >h?ndStC??kW??Z?6?!H??m\??$?~?ًo:?w?>?
                                                                             كo{?a???"L?"???4M'S??????9'??^??qZ?/USO???????^C+?hM??J&G@Ӳy???lt?o߫?c՚?
                                                                    ? ??5?"?Y?i\?΁?'&??.?<?ER/?dE?oc?ግ#?f45@?   ??B:K?@8?i??
                  ??s??_???雭??m?N?|??9}p?????_?A??pX6?5~B?$?&???ti??e??Y)%$?bT?3li?
    ??????P???4?43Y???P??1???KF????ۑ??5>?)?@????r??y??????[?:V???ͦ#??wQ?HB??d(??B
                                                                               a?cĪ?L"J??itTy?8?;(???Gx?_?^?[???????%׎??ŷ??Q???麲?ua??n?7???
                                                           Q???H^e?O?Q?u6?S??u
                                                                              ?2??%vX
     ???^?*l
O?????ޭˀq,>??S???%?L??d????B???1CZ??$M??9??P
                                            'w????\/????]???.r#???E|!?3?>_?o?a?۾?d?1Z?ӑ???z???'?=??????~+??cjJ?tO%mN?????
                                         |??-???bW?O+
o?
  ^?
    I?H?.?;???S?]?i_s9?*p???.7U^??s.?3u?

有人可以告诉我我做错了什么吗?我是否需要指定我正在使用的编码类型?我试图用getssysread和另一个我不记得的文件来阅读该文件。

2 个答案:

答案 0 :(得分:1)

我不完全确定为什么,但我相信这是导致问题的'./json'路径。我在Windows XP机器上尝试了这个脚本并得到了类似的结果。

但是,当我重写脚本以包含File.dirname(__FILE__)而不是'./'时,它才起作用。我还清理了一些代码。

class ServiceCalls 

    def get_file
        dirname = File.join(File.dirname(__FILE__), 'json')
        Dir.entries(dirname).each do |file|
            unless file.start_with? '.'
                File.open(File.join(dirname, file), 'r') {|f| puts f.read}
            end
        end
    end
end

sc = ServiceCalls.new
sc.get_file

__FILE__是当前脚本的路径。 File.join使用系统无关的路径分隔符。 File.open,如果你传递一个块,实际上会在你完成块时为你关闭文件。 String#start_with?比使用[0,1]获取字符串的第一个元素更简洁。

答案 1 :(得分:0)

试试这个:

Dir.entries('./json').each do |mFile|
  next if ['.', '..'].include?(mFile)
  self.sendServiceRequest(mFile)