다음 예에서는 확장자 -
Example
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class Demo { public static void main(String[] args) throws IOException { Stream<Path> path = Files.walk(Paths.get("D:\ExampleDirectory")); System.out.println("List of PDF files:"); path = path.filter(var -> var.toString().endsWith(".pdf")); path.forEach(System.out::println); path = Files.walk(Paths.get("D:\ExampleDirectory")); System.out.println("List of JPG files:"); path = path.filter(var -> var.toString().endsWith(".jpg")); path.forEach(System.out::println); path = Files.walk(Paths.get("D:\ExampleDirectory")); System.out.println("List of text files:"); path = path.filter(var -> var.toString().endsWith(".txt")); path.forEach(System.out::println); path = Files.walk(Paths.get("D:\ExampleDirectory")); System.out.println("List of word files:"); path = path.filter(var -> var.toString().endsWith(".docx")); path.forEach(System.out::println); } }
Output
List of PDF files: D:\ExampleDirectory\demo1.pdf D:\ExampleDirectory\demo2.pdf List of JPG files: D:\ExampleDirectory\sample_jpeg1.jpg D:\ExampleDirectory\sample_jpeg2.jpg List of text files: D:\ExampleDirectory\sample1.txt D:\ExampleDirectory\sample2.txt D:\ExampleDirectory\sample3.txt List of word files: D:\ExampleDirectory\test1.docx D:\ExampleDirectory\test2.docx
다음 예에서는 확장자 -
에 따라 디렉터리에 있는 PDF 파일의 이름을 인쇄합니다. 예
import java.io.File; import java.io.FilenameFilter; import java.io.IOException; public class MyExample{ public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\ExampleDirectory"); //Creating filter for jpg files FilenameFilter jpgFilefilter = new FilenameFilter(){ public boolean accept(File dir, String name) { String lowercaseName = name.toLowerCase(); if (lowercaseName.endsWith(".pdf")) { return true; } else { return false; } } }; String imageFilesList[] = directoryPath.list(jpgFilefilter); System.out.println("List of the jpeg files in the specified directory:"); for(String fileName : imageFilesList) { System.out.println(fileName); } } }
Output
List of the jpeg files in the specified directory: demo1.pdf demo2.pdf
위 내용은 Java에서 특정 파일 확장자를 가진 디렉토리를 검색하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!
성명
이 기사는 tutorialspoint에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
3 몇 주 전ByDDD
Windows 11 KB5054979의 새로운 기능 및 업데이트 문제를 해결하는 방법
2 몇 주 전ByDDD
Will R.E.P.O. 크로스 플레이가 있습니까?
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

WebStorm Mac 버전
유용한 JavaScript 개발 도구

Dreamweaver Mac版
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
