簡體   English   中英

如何從輸入流加載文件?

[英]How to load the file from the inputstream?

我有以下代碼:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.groovy");

我需要將此資源傳遞給名為parse的方法,該方法將File作為param,因此這樣做會失敗:

new GroovyShell().parse(new FileInputStream(in));

如何在java中將FileInputStream轉換為File

您實際上可以使用: GroovyShell #parse(Reader)方法:

Script result = new GroovyShell().parse(new InputStreamReader(in));

用這個 :

try {
    URL url = this.getClass().getClassLoader().getResource("test.groovy");
    File file = new File(url.toURI());
    new Shell().parse(file);
} catch(...){...}

如果您知道“test.groovy”的確切路徑,請執行以下操作:

new Shell().parse(new File("path/to/test.groovy"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM