Refactored code.

This commit is contained in:
Christian P. MOMON 2021-04-22 20:21:22 +02:00
parent 541e0d9254
commit c3a6fc9841
5 changed files with 301 additions and 424 deletions

View File

@ -40,6 +40,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.logar.app.anonymizer.Anonymizer; import fr.devinsy.logar.app.anonymizer.Anonymizer;
import fr.devinsy.logar.app.log.Log;
import fr.devinsy.logar.app.log.LogUtils; import fr.devinsy.logar.app.log.LogUtils;
/** /**
@ -100,14 +101,7 @@ public final class Logar
logger.info("file count={}", files.size()); logger.info("file count={}", files.size());
for (File file : files) for (File file : files)
{ {
if (file.getName().contains("access")) anonymizer.anonymize(file);
{
anonymizer.anonymize(file);
}
else if (file.getName().contains("error"))
{
//
}
} }
if (mapFile != null) if (mapFile != null)
@ -159,74 +153,61 @@ public final class Logar
YearMonth targetYearMonth = YearMonth.now().minusMonths(1); YearMonth targetYearMonth = YearMonth.now().minusMonths(1);
Stats counter = new Stats(); Stats counter = new Stats();
for (File directory : Files.of(source).sortByName()) for (File directory : Files.of(source).removeHidden().keepDirectoriesOnly().sortByName())
{ {
if ((directory.isDirectory()) && (!StringUtils.equalsAny(directory.getName(), ".", ".."))) String targetFileName = String.format("%s-access-%s.log.gz", directory.getName(), targetYearMonth.toString());
File targetDirectory = new File(target, directory.getName());
targetDirectory.mkdirs();
File targetFile = new File(targetDirectory, targetFileName);
logger.info("== {} -> {}", directory.getName(), targetFile.getAbsoluteFile());
PrintWriter out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(targetFile)));
for (File file : Files.of(directory).sortByName())
{ {
String targetFileName = String.format("%s-access-%s.log.gz", directory.getName(), targetYearMonth.toString()); if ((!file.isDirectory()) && (file.getName().contains("access")))
File targetDirectory = new File(target, directory.getName());
targetDirectory.mkdirs();
File targetFile = new File(targetDirectory, targetFileName);
logger.info("== {} -> {}", directory.getName(), targetFile.getAbsoluteFile());
PrintWriter out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(targetFile)));
for (File file : Files.of(directory).sortByName())
{ {
if ((!file.isDirectory()) && (file.getName().contains("access"))) logger.info(file.getName());
try
{ {
logger.info(file.getName()); LineIterator iterator = new LineIterator(file);
try while (iterator.hasNext())
{ {
LineIterator iterator = new LineIterator(file); String line = iterator.next();
while (iterator.hasNext()) counter.incLineCount();
try
{ {
String line = iterator.next(); Log log = LogUtils.parseAccessLog(line);
counter.incLineCount(); counter.incSuccessLineCount();
Matcher matcher = nginxAccessLogLinePattern.matcher(line); if (YearMonth.from(log.getDatetime()).equals(targetYearMonth))
if (matcher.matches())
{ {
String value = matcher.group("time"); out.println(line);
try
{
LocalDateTime date = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z").withLocale(Locale.ENGLISH));
counter.incSuccessLineCount();
if (YearMonth.from(date).equals(targetYearMonth))
{
out.println(line);
}
}
catch (DateTimeParseException exception)
{
System.err.println("Date format problem with [" + line + "]");
counter.incErrorLineCount();
}
}
else
{
System.err.println("Not matching line [" + line + "]");
counter.incErrorLineCount();
} }
} }
catch (IllegalArgumentException exception)
{
System.err.println("Bad line format [" + line + "]");
counter.incErrorLineCount();
}
}
counter.incSuccessFileCount(); counter.incSuccessFileCount();
} }
catch (IOException exception) catch (IOException exception)
{ {
System.err.println("Error with file [" + file.getAbsolutePath() + "]"); System.err.println("Error with file [" + file.getAbsolutePath() + "]");
exception.printStackTrace(); exception.printStackTrace();
counter.incErrorFileCount(); counter.incErrorFileCount();
} }
finally finally
{ {
counter.incFileCount(); counter.incFileCount();
}
} }
} }
IOUtils.closeQuietly(out);
} }
IOUtils.closeQuietly(out);
} }
System.out.println("====================================================="); System.out.println("=====================================================");
@ -265,74 +246,60 @@ public final class Logar
Stats counter = new Stats(); Stats counter = new Stats();
counter.start(); counter.start();
for (File directory : Files.of(source).sortByName()) for (File directory : Files.of(source).removeHidden().keepDirectoriesOnly().sortByName())
{ {
if ((directory.isDirectory()) && (!StringUtils.equalsAny(directory.getName(), ".", ".."))) String targetFileName = String.format("%s-error-%s.log.gz", directory.getName(), targetYearMonth.toString());
File targetDirectory = new File(target, directory.getName());
targetDirectory.mkdirs();
File targetFile = new File(targetDirectory, targetFileName);
logger.info("== {} -> {}", directory.getName(), targetFile.getAbsoluteFile());
PrintWriter out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(targetFile)));
for (File file : Files.of(directory).sortByName())
{ {
String targetFileName = String.format("%s-error-%s.log.gz", directory.getName(), targetYearMonth.toString()); if ((!file.isDirectory()) && (file.getName().contains("error")))
File targetDirectory = new File(target, directory.getName());
targetDirectory.mkdirs();
File targetFile = new File(targetDirectory, targetFileName);
logger.info("== {} -> {}", directory.getName(), targetFile.getAbsoluteFile());
PrintWriter out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(targetFile)));
for (File file : Files.of(directory).sortByName())
{ {
if ((!file.isDirectory()) && (file.getName().contains("error"))) // logger.info(file.getName());
try
{ {
// logger.info(file.getName()); LineIterator iterator = new LineIterator(file);
try while (iterator.hasNext())
{ {
LineIterator iterator = new LineIterator(file); String line = iterator.next();
while (iterator.hasNext()) counter.incLineCount();
try
{ {
String line = iterator.next(); Log log = LogUtils.parseErrorLog(line);
counter.incLineCount(); counter.incSuccessLineCount();
Matcher matcher = nginxErrorLogLinePattern.matcher(line); if (YearMonth.from(log.getDatetime()).equals(targetYearMonth))
if (matcher.matches())
{ {
String value = matcher.group("time"); out.println(line);
try
{
LocalDateTime date = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withLocale(Locale.ENGLISH));
counter.incSuccessLineCount();
if (YearMonth.from(date).equals(targetYearMonth))
{
out.println(line);
}
}
catch (DateTimeParseException exception)
{
System.err.println("Date format problem with [" + line + "]");
counter.incErrorLineCount();
}
}
else
{
System.err.println("Not matching line [" + line + "]");
counter.incErrorLineCount();
} }
} }
catch (IllegalArgumentException exception)
counter.incSuccessLineCount(); {
} System.err.println("Bad line format [" + line + "]");
catch (IOException exception) counter.incErrorLineCount();
{ }
System.err.println("Error with file [" + file.getAbsolutePath() + "]");
exception.printStackTrace();
counter.incErrorFileCount();
}
finally
{
counter.incFileCount();
} }
counter.incSuccessFileCount();
}
catch (IOException exception)
{
System.err.println("Error with file [" + file.getAbsolutePath() + "]");
exception.printStackTrace();
counter.incErrorFileCount();
}
finally
{
counter.incFileCount();
} }
} }
IOUtils.closeQuietly(out);
} }
IOUtils.closeQuietly(out);
} }
System.out.println("====================================================="); System.out.println("=====================================================");
@ -341,51 +308,13 @@ public final class Logar
} }
} }
/**
* Check.
*
* @param source
* the source
*/
public static void check(final File source)
{
if (source != null)
{
if (source.isFile())
{
if (source.getName().contains("access"))
{
checkAccessFiles(source);
}
else if (source.getName().contains("error"))
{
checkErrorFiles(source);
}
}
else
{
for (File file : Files.of(source).removeHidden().sortByName())
{
if (file.isDirectory())
{
check(file);
}
else if (StringUtils.endsWithAny(file.getName(), ".log", ".gz", ".1", ".2"))
{
check(file);
}
}
}
}
}
/** /**
* Check access files. * Check access files.
* *
* @param file * @param file
* the source * the source
*/ */
public static void checkAccessFiles(final File file) public static void checkLogFile(final File file)
{ {
if (file == null) if (file == null)
{ {
@ -398,6 +327,15 @@ public final class Logar
else else
{ {
System.out.println("== Check access log for [" + file.getName() + "]"); System.out.println("== Check access log for [" + file.getName() + "]");
boolean isAccessFile;
if (file.getName().contains("access"))
{
isAccessFile = true;
}
else
{
isAccessFile = false;
}
int lineCount = 0; int lineCount = 0;
int badLineCount = 0; int badLineCount = 0;
@ -411,7 +349,14 @@ public final class Logar
try try
{ {
LogUtils.parseAccessLog(line).getDatetime(); if (isAccessFile)
{
LogUtils.parseAccessLog(line).getDatetime();
}
else
{
LogUtils.parseErrorLog(line).getDatetime();
}
} }
catch (IllegalArgumentException exception) catch (IllegalArgumentException exception)
{ {
@ -440,55 +385,75 @@ public final class Logar
} }
/** /**
* Check error files. * Check.
* *
* @param file * @param source
* the file * the source
*/ */
public static void checkErrorFiles(final File file) public static void checkLogFiles(final File source)
{
Files files = FilesUtils.searchFileRecursively(source, ".log", ".log.gz", ".1", ".2", ".3").removeHidden().sortByName();
for (File file : files)
{
checkLogFile(file);
}
}
/**
* Check sort for access files.
*
* @param source
* the source
* @throws IOException
*/
public static void checkSort(final File file) throws IOException
{ {
if (file == null) if (file == null)
{ {
throw new IllegalArgumentException("Null parameter."); throw new IllegalArgumentException("Null parameter [source]");
} }
else if (!file.isFile()) else if (!file.isFile())
{ {
throw new IllegalArgumentException("Parameter is not a file."); throw new IllegalArgumentException("Source parameter is not a file.");
} }
else else
{ {
System.out.println("== Check error log for [" + file.getName() + "]"); boolean isAccessFile;
if (file.getName().contains("access"))
{
isAccessFile = true;
}
else
{
isAccessFile = false;
}
System.out.println("== Check sort for [" + file.getName() + "]");
LocalDateTime currentDate = null;
int lineCount = 0; int lineCount = 0;
int badLineCount = 0; int badLineCount = 0;
try LineIterator iterator = new LineIterator(file);
while (iterator.hasNext())
{ {
LineIterator iterator = new LineIterator(file); String line = iterator.next();
while (iterator.hasNext()) lineCount += 1;
LocalDateTime date;
if (isAccessFile)
{ {
String line = iterator.next(); date = LogUtils.parseAccessLog(line).getDatetime();
lineCount += 1;
try
{
LogUtils.parseErrorLog(line).getDatetime();
}
catch (IllegalArgumentException exception)
{
System.out.println("Bad format line: " + line);
badLineCount += 1;
}
catch (DateTimeParseException exception)
{
System.out.println("Bad datetime format: " + line);
badLineCount += 1;
}
} }
} else
catch (IOException exception) {
{ date = LogUtils.parseErrorLog(line).getDatetime();
System.err.println("Error with file [" + file.getAbsolutePath() + "]"); }
exception.printStackTrace();
if ((currentDate != null) && (date.isBefore(currentDate)))
{
System.out.println(String.format("break detected: %d %s", lineCount, currentDate.toString()));
badLineCount += 1;
}
currentDate = date;
} }
if (badLineCount > 0) if (badLineCount > 0)
@ -505,116 +470,13 @@ public final class Logar
* the source * the source
* @throws IOException * @throws IOException
*/ */
public static void checkSort(final File source) throws IOException public static void checkSorts(final File source) throws IOException
{ {
if (source != null) Files files = FilesUtils.searchFileRecursively(source, ".log", ".log.gz").removeHidden().sortByName();
{
if (source.isFile())
{
if (source.getName().contains("access"))
{
checkSortForAccessFiles(source);
}
else if (source.getName().contains("error"))
{
checkSortForErrorFiles(source);
}
}
else
{
for (File file : Files.of(source).removeHidden().sortByName())
{
checkSort(file);
}
}
}
}
/** for (File file : files)
* Check sort for access files.
*
* @param source
* the source
* @throws IOException
*/
public static void checkSortForAccessFiles(final File file) throws IOException
{
if (file == null)
{ {
throw new IllegalArgumentException("Null parameter [source]"); checkSort(file);
}
else if (!file.isFile())
{
throw new IllegalArgumentException("Source parameter is not a file.");
}
else
{
System.out.println("== Check sort for [" + file.getName() + "]");
LocalDateTime currentDate = null;
int lineCount = 0;
int badLineCount = 0;
LineIterator iterator = new LineIterator(file);
while (iterator.hasNext())
{
String line = iterator.next();
lineCount += 1;
LocalDateTime date = LogUtils.parseAccessLog(line).getDatetime();
if ((currentDate != null) && (date.isBefore(currentDate)))
{
System.out.println(String.format("break detected: %d %s", lineCount, currentDate.toString()));
badLineCount += 1;
}
currentDate = date;
}
if (badLineCount > 0)
{
System.out.println("Bad line count: " + badLineCount + "/" + lineCount);
}
}
}
/**
* Check sort for error files.
*
* @param source
* the source
* @throws IOException
*/
public static void checkSortForErrorFiles(final File file) throws IOException
{
if (file == null)
{
throw new IllegalArgumentException("Null parameter [source]");
}
else if (!file.isFile())
{
throw new IllegalArgumentException("Source parameter is not a file.");
}
else
{
System.out.println("== Check sort for [" + file.getName() + "]");
LocalDateTime currentDate = null;
int lineCount = 0;
int badLineCount = 0;
LineIterator iterator = new LineIterator(file);
while (iterator.hasNext())
{
String line = iterator.next();
lineCount += 1;
LocalDateTime date = LogUtils.parseErrorLog(line).getDatetime();
if ((currentDate != null) && (date.isBefore(currentDate)))
{
System.out.println(String.format("break detected: %d %s", lineCount, currentDate.toString()));
badLineCount += 1;
}
currentDate = date;
}
if (badLineCount > 0)
{
System.out.println("Bad line count: " + badLineCount + "/" + lineCount);
}
} }
} }
@ -628,75 +490,12 @@ public final class Logar
*/ */
public static void sort(final File source) throws IOException public static void sort(final File source) throws IOException
{ {
if (source != null) Files files = FilesUtils.searchFileRecursively(source, ".log", ".log.gz").removeHidden().sortByName();
{
if (source.isFile())
{
if (source.getName().contains("access"))
{
sortAccessFiles(source);
}
else if (source.getName().contains("error"))
{
sortErrorFiles(source);
}
}
else
{
for (File file : Files.of(source).removeHidden().sortByName())
{
sort(file);
}
}
}
}
/** for (File file : files)
* Sort access files.
*
* @param file
* the file
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void sortAccessFiles(final File file) throws IOException
{
if (file == null)
{
throw new IllegalArgumentException("Null parameter [source]");
}
else if (!file.isFile())
{
throw new IllegalArgumentException("Source parameter is not a file.");
}
else
{ {
System.out.println("== Sort for [" + file.getName() + "]"); System.out.println("== Sort for [" + file.getName() + "]");
LogUtils.sortAccessLog(file); LogUtils.sortLogFile(file);
}
}
/**
* Sort error files.
*
* @param file
* the file
* @throws IOException
*/
public static void sortErrorFiles(final File file) throws IOException
{
if (file == null)
{
throw new IllegalArgumentException("Null parameter [source]");
}
else if (!file.isFile())
{
throw new IllegalArgumentException("Source parameter is not a file.");
}
else
{
System.out.println("== Sort for [" + file.getName() + "]");
LogUtils.sortErrorLog(file);
} }
} }

View File

@ -30,11 +30,17 @@ public final class Log
{ {
private static Logger logger = LoggerFactory.getLogger(Log.class); private static Logger logger = LoggerFactory.getLogger(Log.class);
// Generic attributes.
private String line; private String line;
private LocalDateTime datetime; private LocalDateTime datetime;
// Specific access log attributes.
private String ip; private String ip;
private String user; private String user;
// Specific error log attributes.
// private String message;
/** /**
* Instantiates a new log. * Instantiates a new log.
*/ */

View File

@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -111,6 +112,43 @@ public final class LogUtils
return result; return result;
} }
/**
* Load log file.
*
* @param file
* the file
* @return the logs
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static Logs loadLogFile(final File file) throws IOException
{
Logs result;
if (file == null)
{
throw new IllegalArgumentException("Null parameter.");
}
else
{
if (file.getName().contains("access"))
{
result = loadAccessLog(file);
}
else if (file.getName().contains("error"))
{
result = loadErrorLog(file);
}
else
{
throw new IllegalArgumentException("Bad named file (missing access or error).");
}
}
//
return result;
}
/** /**
* From access log. * From access log.
* *
@ -122,20 +160,27 @@ public final class LogUtils
{ {
Log result; Log result;
Matcher matcher = NGINX_ACCESSLOG_LINE_PATTERN.matcher(line); try
if (matcher.matches())
{ {
String value = matcher.group("time"); Matcher matcher = NGINX_ACCESSLOG_LINE_PATTERN.matcher(line);
LocalDateTime date = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z").withLocale(Locale.ENGLISH)); if (matcher.matches())
{
String value = matcher.group("time");
LocalDateTime date = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z").withLocale(Locale.ENGLISH));
String ip = matcher.group("remoteAddress").trim(); String ip = matcher.group("remoteAddress").trim();
String user = matcher.group("remoteUser").trim(); String user = matcher.group("remoteUser").trim();
result = new Log(line, date, ip, user); result = new Log(line, date, ip, user);
}
else
{
throw new IllegalArgumentException("Bad line format: " + line);
}
} }
else catch (DateTimeParseException exception)
{ {
throw new IllegalArgumentException("Bad line format: " + line); throw new IllegalArgumentException("Bad line format (date): " + line);
} }
// //
@ -153,17 +198,24 @@ public final class LogUtils
{ {
Log result; Log result;
Matcher matcher = NGINX_ERRORLOG_LINE_PATTERN.matcher(line); try
if (matcher.matches())
{ {
String value = matcher.group("time"); Matcher matcher = NGINX_ERRORLOG_LINE_PATTERN.matcher(line);
LocalDateTime date = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withLocale(Locale.ENGLISH)); if (matcher.matches())
{
String value = matcher.group("time");
LocalDateTime date = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withLocale(Locale.ENGLISH));
result = new Log(line, date); result = new Log(line, date);
}
else
{
throw new IllegalArgumentException("Bad line format: " + line);
}
} }
else catch (DateTimeParseException exception)
{ {
throw new IllegalArgumentException("Bad line format: " + line); throw new IllegalArgumentException("Bad line format (date): " + line);
} }
// //
@ -183,7 +235,14 @@ public final class LogUtils
PrintWriter out = null; PrintWriter out = null;
try try
{ {
out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(target))); if (target.getName().endsWith(".gz"))
{
out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(target)));
}
else
{
out = new PrintWriter(new FileOutputStream(target));
}
for (Log log : logs) for (Log log : logs)
{ {
@ -203,61 +262,24 @@ public final class LogUtils
* the target * the target
* @throws IOException * @throws IOException
*/ */
public static void sortAccessLog(final File file) throws IOException public static void sortLogFile(final File file) throws IOException
{ {
File work = new File(file.getParent(), file.getName() + ".tmp"); File workFile = new File(file.getParent(), file.getName() + ".tmp");
Logs logs = loadAccessLog(file); Logs logs = loadLogFile(file);
logs.sortByDatetime(); logs.sortByDatetime();
saveLogs(work, logs); saveLogs(workFile, logs);
File backup = new File(file.getParentFile(), file.getName() + ".bak"); File backup = new File(file.getParentFile(), file.getName() + ".bak");
if (file.renameTo(backup)) if (file.renameTo(backup))
{ {
if (!work.renameTo(file)) if (!workFile.renameTo(file))
{
backup.renameTo(file);
}
}
try
{
String out = CmdExecUtils.run("/bin/bash -c \"zcat " + file.getAbsolutePath() + "| sort | sha1sum \"");
System.out.print(out);
out = CmdExecUtils.run("/bin/bash -c \"zcat " + file.getAbsolutePath() + ".bak | sort | sha1sum \"");
System.out.println(out);
}
catch (CmdExecException exception)
{
exception.printStackTrace();
}
}
/**
* Sort error log.
*
* @param file
* the file
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void sortErrorLog(final File file) throws IOException
{
File work = new File(file.getParent(), file.getName() + ".tmp");
Logs logs = loadErrorLog(file);
logs.sortByDatetime();
saveLogs(work, logs);
File backup = new File(file.getParentFile(), file.getName() + ".bak");
if (file.renameTo(backup))
{
if (!work.renameTo(file))
{ {
backup.renameTo(file); backup.renameTo(file);
} }
} }
// Check.
try try
{ {
String out = CmdExecUtils.run("/bin/bash -c \"zcat " + file.getAbsolutePath() + "| sort | sha1sum \""); String out = CmdExecUtils.run("/bin/bash -c \"zcat " + file.getAbsolutePath() + "| sort | sha1sum \"");

View File

@ -56,8 +56,8 @@ public final class LogarCLI
message.appendln(" logar [ -v | -version | --version ]"); message.appendln(" logar [ -v | -version | --version ]");
message.appendln(" logar anonymize fileordirectory [maptable] anonymize ip and login"); message.appendln(" logar anonymize fileordirectory [maptable] anonymize ip and login");
message.appendln(" logar archive source target archive previous month"); message.appendln(" logar archive source target archive previous month");
message.appendln(" logar checksort fileordirectory check sort of an access log file");
message.appendln(" logar check fileordirectory census bad format line in log files"); message.appendln(" logar check fileordirectory census bad format line in log files");
message.appendln(" logar checksort fileordirectory check sort of an access log file");
message.appendln(" logar sort fileordirectory sort log files by datetime"); message.appendln(" logar sort fileordirectory sort log files by datetime");
message.appendln(" logar testarchive source test archive"); message.appendln(" logar testarchive source test archive");
@ -207,13 +207,13 @@ public final class LogarCLI
{ {
File source = new File(args[1]); File source = new File(args[1]);
Logar.check(source); Logar.checkLogFiles(source);
} }
else if (isMatching(args, "checksort", "\\s*\\S+\\s*")) else if (isMatching(args, "checksort", "\\s*\\S+\\s*"))
{ {
File source = new File(args[1]); File source = new File(args[1]);
Logar.checkSort(source); Logar.checkSorts(source);
} }
else if (isMatching(args, "sort", "\\s*\\S+\\s*")) else if (isMatching(args, "sort", "\\s*\\S+\\s*"))
{ {

View File

@ -48,6 +48,31 @@ public class Files extends ArrayList<File>
super(initialCapacity); super(initialCapacity);
} }
/**
* Keep directories.
*
* @return the files
*/
public Files keepDirectoriesOnly()
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (!file.isDirectory())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/** /**
* Removes the containing. * Removes the containing.
* *
@ -75,6 +100,31 @@ public class Files extends ArrayList<File>
return result; return result;
} }
/**
* Removes the file type.
*
* @return the files
*/
public Files removeFileType()
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (file.isFile())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/** /**
* Removes the hidden. * Removes the hidden.
* *