Tags down


Java NIO Search file in a directory

By : Tony_Z
Date : October 15 2020, 08:10 AM
I think the issue was by ths following , There are two things to be changed:
To "find(first) file with given glob String" you need to finish walking the tree if you encounter the file, thus if a match is given. And you need to store the matching path as result. The result of Files.walkFileTree itself is the "the starting file" (JavaDoc). That's a Path pointing to the location.
code :
public static String match(String glob, String location) throws IOException {
    StringBuilder result = new StringBuilder();
    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);
    Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            if (pathMatcher.matches(path)) {
                return FileVisitResult.TERMINATE;
            return FileVisitResult.CONTINUE;

    return result.toString();
public static Optional<Path> match(String glob, String location) throws IOException {
    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);
    return Files.walk(Paths.get(location)).filter(pathMatcher::matches).findFirst();

Share : facebook icon twitter icon

How to make GCC search for headers in a directory before the current source file's directory?

By : user2107106
Date : March 29 2020, 07:55 AM
should help you out I am using GCC precompiled headers in my project with multi-architecture build, but things break down when I try to place it in a directory different from current source's directory. , I have found a workaround.
code :
gcc a.h -o pchdir.i686/a-precompiled.h.gch
gcc -Ipchdir.i686 -include a-precompiled.h <other arguments> <source>

java: search file according to its name in directory and subdirectories

By : user3153780
Date : March 29 2020, 07:55 AM
I wish this helpful for you I need a to find file according to its name in directory tree. And then show a path to this file. I found something like this, but it search according extension. Could anybody help me how can I rework this code to my needs...thanks
code :
public class Test {
    public static void main(String[] args) {
        File root = new File("c:\\test");
        String fileName = "a.txt";
        try {
            boolean recursive = true;

            Collection files = FileUtils.listFiles(root, null, recursive);

            for (Iterator iterator = files.iterator(); iterator.hasNext();) {
                File file = (File) iterator.next();
                if (file.getName().equals(fileName))
        } catch (Exception e) {

How to search a hard drive for a directory name using Windows batch file and strip only the directory name

By : user2857206
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I am building a .bat file that requires me to find a directory on a hard drive. , This should get you started:
code :
FOR /F "delims=" %%I IN ('dir woff.exe /s /b') DO SET myvar=%%~dI%%~pI
SET myvar=%myvar:~0,-1%

Search a directory for a .txt file, without needing full pathname. - Java

By : Rob Rob
Date : March 29 2020, 07:55 AM
this will help As stated in the comments, there are several tools already available to search files in a directory. However, to answer your question, I wrote a simple program that should do what you are looking for:
code :
public static void main(String[] args) {
    // Get the absolute path from where your application has initialized
    File workingDirectory = new File(System.getProperty("user.dir"));
    // Get user input
    String query = new Scanner(System.in).next();
    // Perform a search in the working directory
    List<File> files = search(workingDirectory, query);
    // Check if there are no matching files
    if (files.isEmpty()) {
        System.out.println("No files found in " + workingDirectory.getPath() + " that match '"
                + query + "'");
    // print all the files that matched the query
    for (File file : files) {

public static List<File> search(File file, String query) {
    List<File> fileList = new ArrayList<File>();
    // Get all the files in this directory
    File[] files = file.listFiles();
    if (files != null) {
        for (File f : files) {
            if (f.isDirectory()) {
                // use recursion to search in all directories for the file
                fileList.addAll(search(f, query));
            } else if (f.getName().toLowerCase().contains(query.toLowerCase())) {
                // if the filename matches the query, add it to the list
    return fileList;

How to search for docx file and unzip it in the given directory using java?

By : july
Date : March 29 2020, 07:55 AM
Any of those help For search files by extensions you can use Apache Commons IO library:
code :
FileUtils.iterateFiles(File directory, String[] extensions, boolean recursive)
try(ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
    ZipArchiveEntry zipEntry;

    while ((zipEntry = zipArchiveInputStream.getNextZipEntry()) != null){
        String fileName = zipEntry.getName();

        final File file = new File(fileName);


        try(FileOutputStream fileOutputStream = new FileOutputStream(file.getAbsolutePath())) {(file.read.buffer)
            try(BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream, 1024)) {
                int n = 0;

                byte[] content = new byte[1024];

                while ((n = zipArchiveInputStream.read(content)) != -1) {
                    fileOutputStream.write(content, 0, n);

Related Posts Related Posts :
  • Kafka consumer returns no records
  • Timediff as resultset
  • Deadlock happens if I use lambda in parallel stream but it doesn't happen if I use anonymous class instead?
  • Check if object does throw a NullPointerException
  • I'm trying to understand how "calling an object's method" works in Java, but it's confusing me
  • Honeypot sample test Odd Numbers question
  • How to query Oracle via JDBC for an intersection
  • i keep getting an error: "testapp has stopped" when i go from Main2Activity.java to Main3Activity.java
  • JPA: Adding new entry to a many-to-many taking long time
  • Bouncy Castle PQC XMSS signing: NullPointerException after retrieving SecretKey from KeyStore
  • Extremely slow built-in AES encryption with Java
  • Remove duplicates items from arraylist and hashmap
  • Traversing a Table & Clicking Links Within Using Selenium
  • How to make a list of contents longer than the parent screen?
  • How to get tools.jar for OpenJDK 11 on Windows?
  • Java: HashMap Into Nested JSON Object
  • addLast method for a deque with only one sentinel node
  • Oracle datetime which does not exist in SQL Server
  • Thread's ThreadLocals cleaning
  • String index out of range: 0 Error For using RedirectAttributes in Spring Boot
  • How to pass an array to POST API?
  • What is the spring-boot-configuration-processor ? Why do people exclude libraries from it? Why is it invisible in depend
  • concurrency in a single Processor instance and concurrentConsumers
  • indexOf + substring in Java
  • TestNG.xml - New browser session does not open after first one is closed
  • How to deterministically map sequential integers to uniformly distributed doubles
  • Problem using oracle-serverjre:8 in dockerfile
  • PDF 'Itext User Agent' cache size and how to clear it
  • set proxy for firefox using sikuli in java
  • UnrecognizedPropertyException when using Jackson, @JsonIdentityInfo and @JsonDeserialize with a lombok Builder
  • Java Array containing reference to object
  • How can I add bundled jars to my classpath in an Ant build task?
  • cannot be cast to Java
  • SseEmitter with proxy
  • Microservices broadcast messages
  • Unable log in to the index.html
  • Simple Java 11 JavaFXprogram, Error when run from JAR file (Windows)
  • randomly displaying circles within gridPane cells in javaFX
  • How to force client to implement multiple generic interfaces with same type
  • Set transparent background on JList
  • How to print the Execution Plan for batch processing in Flink?
  • How to specify which version of a concrete implementation for an abstract class to autowire in Spring?
  • Regular expression get the third element from a string
  • How do I use client certificates in a client java application?
  • Allow dashes in email validations
  • Unable to display Json data
  • Add constraints to lines in javaFX
  • How do you include a classpath wildcard as an argument to a java bash call?
  • How to use classes from one project on another project in java?
  • How to parse a column that has a custom json format from a spark DataFrame
  • How to parse entire DOM elements and follow changes in DOM using selenium webdriver in java
  • java set object field equal another field upon instantation
  • Java Threads Object Lock
  • Euro Currency getting Question mark?
  • Running out of pools using CompleteableFuture and Spring Transaction
  • Drools can not use a generic type for a function's parameter in DRL
  • Create a new ArrayList with all the elements of another ArrayList except one in one line of code
  • log4j could not create log file in spring boot resti api
  • Android BLE discovery issue
  • Adding Java final keyword to working method that builds instances inside a loop
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org