SoFunction
Updated on 2025-04-04

Java8 uses Function to read files

First, we need a FileCloseAble class to handle file write operations.

import ;
import ;
import ;

public class FileCloseAble<T> implements AutoCloseable {
    private Consumer<List<T>> writer;
    private int pageSize;
    private List<T> buffer;

    public FileCloseAble(Consumer<List<T>> writer, int pageSize) {
         = writer;
         = pageSize;
         = new ArrayList<>((1, pageSize));
    }

    public void add(T element) {
        (element);
        if (() >= pageSize) {
            flush();
        }
    }

    private void flush() {
        if (!()) {
            (new ArrayList<>(buffer));
            ();
        }
    }

    @Override
    public void close() {
        flush();
    }
}

The FileContext class to store the context information of the file.

public class FileContext {
    private File file;
    private Charset charset;
    private int skipHeader;
    private String batchDate;
    private int pageSize;
    private boolean deleteFlag;
    private String filterLineHeader;

    // constructor, getter and setter omitted
    public File getFile() {
        return file;
    }

    public void setFile(File file) {
         = file;
    }

    public Charset getCharset() {
        return charset;
    }

    public void setCharset(Charset charset) {
         = charset;
    }

    public int getSkipHeader() {
        return skipHeader;
    }

    public void setSkipHeader(int skipHeader) {
         = skipHeader;
    }

    public String getBatchDate() {
        return batchDate;
    }

    public void setBatchDate(String batchDate) {
         = batchDate;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
         = pageSize;
    }

    public boolean isDeleteFlag() {
        return deleteFlag;
    }

    public void setDeleteFlag(boolean deleteFlag) {
         = deleteFlag;
    }

    public String getFilterLineHeader() {
        return filterLineHeader;
    }

    public void setFilterLineHeader(String filterLineHeader) {
         = filterLineHeader;
    }
}

FileHandler class

import ;
import ;
import ;
import .slf4j.Slf4j;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class FileHandler&lt;T, R&gt; {

    private FileContext context;
    private BiFunction&lt;String, String, T&gt; reader; // Read logic    private Consumer&lt;List&lt;T&gt;&gt; writer;// Write logic    private LineFilter lineFilter; // Added filtering logic
    public void execute() throws Exception {
        File file = ();
        try (
                Stream&lt;String&gt; streams = ((()), ());
                FileCloseAble&lt;T&gt; files = new FileCloseAble&lt;&gt;(writer, ())
        ) {
            (())
                    .filter(())
                    .map(e -&gt; (e, ()))
                    .forEach(files::add);
        } catch (Exception e) {
            ("Failed to parse the file: file: {}, error: {}", file, e);
            throw new Exception("File handling exception", e);
        } finally {
            deleteFileIfNecessary(file);
        }
    }

    private void deleteFileIfNecessary(File file) {
        if (() &amp;&amp; ()) {
            try {
                boolean deleted = (());
                if (deleted) {
                    ("File deleted: {}", file);
                } else {
                    ("The file does not exist,Unable to delete: {}", file);
                }
            } catch (Exception e) {
                ("Failed to delete the file: file: {}, error: {}", file, e);
            }
        }
    }
}

Define a filter function interface

Defines a Predicate interface that represents the filtering logic for each row.

import ;

public interface LineFilter {
    Predicate<String> getLinePredicate();
}

Implementing LineFilter interface

Create a class that implements the LineFilter interface to define specific filtering logic.

import ;

public class CustomLineFilter implements LineFilter {
    private String filterLineHeader;

    public CustomLineFilter(String filterLineHeader) {
         = filterLineHeader;
    }

    @Override
    public Predicate<String> getLinePredicate() {
        return line -> !(filterLineHeader);
    }
}

Use the FileHandler class

import ;
import ;
import ;
import ;
import ;

public class FileHandlerExample {
    public static void main(String[] args) {
        // Create file context        FileContext context = new FileContext();
        File file = new File("D:\\temp\\");
        (file);
        (StandardCharsets.UTF_8);
        (0);
        ("20220101");
        (100);
        (false);

        // Define the parsing method        BiFunction&lt;String, String, String&gt; reader = (line, batchDate) -&gt; ();

        // Define the write method        Consumer&lt;List&lt;String&gt;&gt; writer = list -&gt; {
            for (String s : list) {
                (s);
            }
        };

        // Create custom filtering logic        LineFilter lineFilter = new CustomLineFilter("#");

        // Create FileHandler instance        FileHandler&lt;String, Void&gt; fileHandler = FileHandler.&lt;String, Void&gt;builder()
                .context(context)
                .reader(reader)
                .writer(writer)
                .lineFilter(lineFilter) // Set filtering logic                .build();

        // Execute file processing        try {
            ();
        } catch (Exception e) {
            ();
        }
    }
}

You can read and write files happily

This is the end of this article about Java8 using Function to read files. For more related Java8 Function to read files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!