Java에서 File
클래스는 파일 시스템의 "기존"파일을 나타내지 않아도됩니다. 예 :
File f = new File("some_unknown_unexisting_file.bob");
System.out.println(f.exists()); // most likely will print 'false'
또한 클래스는 파일을 현재 작업 디렉토리에서 확인합니다. 할 수 있다면 당신은 내가 가진 File[]
배열을 점점 제안, 귀하의 경우에는
System.out.println(new File(".").getAbsolutePath());
이 디렉토리를 얻을 수 있습니다 : 폴더가 아닌 File
객체의 배열을 반환합니다
File[] files = new File("icons").listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return !f.isDirectory() && f.canRead();
}
});
for (File f : files) {
System.out.println(f.getAbsolutePath());
}
및 당신은 독서를 위해 열 수 있습니다 (이것은 항상 사실이 아니지만 귀하의 경우에는 괜찮습니다).
하지만 list()
를 사용하는이 경우, 다음이 동일합니다 : 당신이 사용하는 아이콘을 만들 수있는 파일 (String[]
을)의 목록과 함께,
File parent = new File("icons");
String[] fileStr = parent.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return !f.isDirectory() && f.canRead();
}
});
for (String f : fileStr) {
System.out.println(new File(parent, f).getAbsolutePath());
}
또한 :
String filename = fileStr[i]; // some file name within the array
ImageIcon icon = new ImageIcon("icons" + File.separator + filename);
또는 귀하의 파일 목록 (File[]
)을 가지고 있다면, 그것은 더 깨끗합니다 :
File file = files[i]; // some file within the File[] array
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
행운을 비네.
아이콘이있는 인용 부호 안에 디렉토리 경로를 넣으십시오. –
나는 당신의 질문이 무엇인지 모르겠습니다. 게시 한 코드는 "icons"디렉토리의 모든 파일 이름을 배열로 반환합니다."개별 파일의 내용을 어떻게 읽습니까?"라는 질문이 있습니다. – jacobm
다시 질문이 무엇입니까? –