我试图使用InputStream获取目录中存在的文件的名称,但它总是返回空:[]
任何人都可以帮我解决这些问题。
项目的结构如下:
@component
新代码:
The code that i made:
// Gets the info.
String path = "/imagens/";
InputStream is = getClass().getResourceAsStream(path);
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
br.lines()
.map(l -> path + "/" + l)
.collect(toCollection(FXCollections::observableArrayList));
// Shows the info.
URL resource = getClass().getResource("/imagens/");
try(InputStream is2 = resource.openStream())
{
System.out.println(observableArrayList());
}
catch (IOException ex)
{
Logger.getLogger(JavaFX_Paths.class.getName()).log(Level.SEVERE, null, ex);
}
最终代码(最后工作:)))) 我使用了InputStream,InputStreamReader和BufferedReader ......与File对象或FileReaders无关。
HBox root = new HBox();
Button btn = null;
ArrayList<String> items = new ArrayList<String>();
String textLine;
try
{
BufferedReader br = new BufferedReader(new FileReader("src/imagens/lista_icones.txt"));
while ((textLine = br.readLine()) != null)
items.add(textLine);
br.close();
}
catch (IOException ex)
{
Logger.getLogger(JavaFX_Paths.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < items.size(); i++)
{
Image img = new Image(getClass().getResourceAsStream("/imagens/" + items.get(i) + ".png"));
btn = new Button();
btn.setId(items.get(i));
btn.setGraphic(new ImageView(img));
root.getChildren().addAll(btn);
}