Java Path File Write nio removeMatchingLines( final Map d1LineMap, final Path tableDir, final String table, final BufferedWriter errors)

Here you can find the source of removeMatchingLines( final Map d1LineMap, final Path tableDir, final String table, final BufferedWriter errors)

Description

remove Matching Lines

License

Open Source License

Declaration

private static boolean removeMatchingLines(
            final Map<String, Integer> d1LineMap, final Path tableDir,
            final String table, final BufferedWriter errors)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;

import java.util.Map;

import java.util.zip.GZIPInputStream;

public class Main {
    private static boolean removeMatchingLines(
            final Map<String, Integer> d1LineMap, final Path tableDir,
            final String table, final BufferedWriter errors)
            throws IOException {
        boolean passed = true;
        try (DirectoryStream<Path> listing = Files
                .newDirectoryStream(tableDir)) {
            for (final Path file : listing) {
                try (BufferedReader in = getReaderForFile(file)) {
                    String line = in.readLine();
                    while (line != null) {
                        if (!d1LineMap.containsKey(line)) {
                            passed = false;
                            errors.write("> " + table + ": " + line);
                        }/*from   w  w  w.  j  av  a  2  s  . co m*/
                        if (d1LineMap.get(line) == 1) {
                            d1LineMap.remove(line);
                        } else {
                            d1LineMap.put(line, d1LineMap.get(line) - 1);
                        }
                        line = in.readLine();
                    }
                }
            }
        }
        return passed;
    }

    private static BufferedReader getReaderForFile(final Path file)
            throws FileNotFoundException, IOException {
        final String filename = file.getFileName().toString().toLowerCase();
        if (filename.endsWith(".gz")) {
            return new BufferedReader(new InputStreamReader(
                    new GZIPInputStream(Files.newInputStream(file))));
        }
        return Files.newBufferedReader(file);
    }
}

Related

  1. newBufferedWriter(Path path)
  2. newBufferedWriter(Path path, OpenOption... options)
  3. newGzipBufferedWriter(Path path)
  4. okayToOverwrite(Path file)
  5. overwriteFile(final String newFilePath, final String oldFilePath)
  6. robustCheckWriteable(Path logFile)
  7. save(Object o, String path)
  8. saveFile(String path, String text)
  9. saveHeaders(Path path, ArrayList headers)