0x001E - Java File Walker Example

Java File Walker Example

0x001E - Java File Walker Example
Photo by Tyler Nix / Unsplash

Our file walker class:

        file_list proc_files = new file_list();
        Stream<Path> file_list  = proc_files.files(myObj.tdir);
        Iterator<Path> f_iterator = file_list.iterator();
        while (f_iterator.hasNext()){
            Path fpath = f_iterator.next();
            String spath = fpath.toString();
            List<Quote> qlist = mreader.csv_process(spath, 1);

And our example calling function:

        csv_reader mreader = new csv_reader();
        file_list proc_files = new file_list();
        Stream<Path> file_list  = proc_files.files(myObj.tdir);
        Iterator<Path> f_iterator = file_list.iterator();
        while (f_iterator.hasNext()){
            Path fpath = f_iterator.next();
            String spath = fpath.toString();
            List<Quote> qlist = mreader.csv_process(spath, 1);
            if (qlist.size() > 0)
            {

            }
            System.out.println("dkdkkd");
        }

Jetbrains excellent Intellij is available in the community edition: - with it we can inspect our code and modify. We can see that file walker also returns the directories, so we can adapt the code easily this way.

        Stream<Path> file_list  = proc_files.files(myObj.tdir);
        if (file_list != null) {
            Iterator<Path> f_iterator = file_list.iterator();
            while (f_iterator.hasNext()) {
                Path fpath = f_iterator.next();
                String spath = fpath.toString();
                List<Quote> qlist = mreader.csv_process(spath, 1);
                if (qlist.size() > 0) {
                    System.out.printf("Processed: %s\n", spath);
                }
            }
        } else
        {
            System.out.printf("File path: %s failed open exiting..\n", myObj.tdir);
            System.exit(-1);
        }

Our import section:

import java.io.IOException;
import java.nio.file.Path;
import java.sql.*;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
Linux Rocks Every Day