2016-12-25 4 views
1

数ヶ月前に私はgradleのマニュアルを読んで、私はリモートでファイルを参照できると思っていました。したがって、httpでホストされています。例えば、ここにインターネット上にホストされているサンプルのJSONファイルされる:gradle - リモートでファイルにアクセスする方法

http://techslides.com/demos/samples/sample.json

ので、私が達成したい簡単な作業ではGradleの中で、このファイルを読み込んで、概念実証として出力をプリントアウトすることです。

はこのように、私のbuild.gradleファイルに私は次のように置く:

allprojects { 
    afterEvaluate { project -> 

     FileCollection collection = files('http://techslides.com/demos/samples/sample.json') 
     if (!collection.isEmpty()) { 
      File file = collection.getSingleFile(); 
      println 'this is the file'+ file.text; 
     } 
    } 
} 

をしかし、私は次のエラーを取得しています:

エラー:(51、0)URLを変換できません 'http://techslides.com/demos/samples/sample.jsonを' にファイル。

答えて

2

あなたは、テキストファイルをダウンロードし、行ずつ、それを読むにはfile:/path/to/file

ようFileでのみfile URL読むことができます、あなたはURLオブジェクトに変換し、Readerでそれを読むことができます:

def line 
'http://techslides.com/demos/samples/sample.json'.toURL().withReader { reader -> 
    while (line = reader.readLine()) { 
     println line 
    } 
} 
関連する問題