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<T, R> { private FileContext context; private BiFunction<String, String, T> reader; // Read logic private Consumer<List<T>> writer;// Write logic private LineFilter lineFilter; // Added filtering logic public void execute() throws Exception { File file = (); try ( Stream<String> streams = ((()), ()); FileCloseAble<T> files = new FileCloseAble<>(writer, ()) ) { (()) .filter(()) .map(e -> (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 (() && ()) { 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<String, String, String> reader = (line, batchDate) -> (); // Define the write method Consumer<List<String>> writer = list -> { for (String s : list) { (s); } }; // Create custom filtering logic LineFilter lineFilter = new CustomLineFilter("#"); // Create FileHandler instance FileHandler<String, Void> fileHandler = FileHandler.<String, Void>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!