建立由來源(例如字串、檔案和封存項目)支援的 TextResource
。
使用範例
def sourcedFromString = resources.text.fromString("some text content") def sourcedFromFile = resources.text.fromFile("path/to/file.txt") task someTask {} // assumption: produces a text file and declares it as output def sourcedFromTask = resources.text.fromFile(someTask) def sourcedFromArchiveEntry = resources.text.fromArchiveEntry("path/to/archive.zip", "path/to/archive/entry.txt") configurations { someConfig } // assumption: contains a single archive def sourcedFromConfiguration = resources.text.fromArchiveEntry(configurations.someConfig, "path/to/archive/entry.txt") def sourceFromUri = resources.text.fromUri("https://example.com/resource") def sourceFromInsecureUri = resources.text.fromInsecureUri("http://example.com/resource")
基於檔案的工廠方法可選擇性地接受字元編碼。如果未指定編碼,則使用平台的預設編碼。
方法 | 描述 |
fromArchiveEntry(archive, path) | 與 |
fromArchiveEntry(archive, entryPath, charset) | 建立由給定封存檔中指定路徑的封存項目支援的文字資源。封存格式根據封存檔的副檔名確定。如果封存格式不受支援或無法確定,則任何存取資源的嘗試都將失敗並出現例外。 |
fromFile(file) | 與 |
fromFile(file, charset) | 建立由給定檔案支援的文字資源。 |
fromInsecureUri(uri) | 建立由給定 URI 支援的文字資源。注意: 此方法允許使用不安全的協定(如 HTTP)。僅當您對風險感到放心時才使用此方法。 |
fromString(string) | 建立由給定字串支援的文字資源。 |
fromUri(uri) | 建立由給定 URI 支援的文字資源。 |
TextResource
fromArchiveEntry
(Object
archive, String
path)
與 fromArchiveEntry(archive, path, Charset.defaultCharset().name())
相同。
TextResource
fromArchiveEntry
(Object
archive, String
entryPath, String
charset)
建立由給定封存檔中指定路徑的封存項目支援的文字資源。封存格式根據封存檔的副檔名確定。如果封存格式不受支援或無法確定,則任何存取資源的嘗試都將失敗並出現例外。
TextResource
fromFile
(Object
file)
與 fromFile(file, Charset.defaultCharset())
相同。
TextResource
fromFile
(Object
file, String
charset)
建立由給定檔案支援的文字資源。
TextResource
fromInsecureUri
(Object
uri)
建立由給定 URI 支援的文字資源。注意: 此方法允許使用不安全的協定(如 HTTP)。僅當您對風險感到放心時才使用此方法。
TextResource
fromString
(String
string)
建立由給定字串支援的文字資源。
TextResource
fromUri
(Object
uri)
建立由給定 URI 支援的文字資源。