Refactored readFirstLine methods.

This commit is contained in:
Christian P. MOMON 2024-07-18 19:23:17 +02:00
parent 4612402014
commit 70f6c9fb02
2 changed files with 120 additions and 38 deletions

View file

@ -19,6 +19,7 @@
package fr.devinsy.statoolinfos.util;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
@ -76,6 +77,112 @@ public class FilesUtils
return result;
}
/**
* Read first line.
*
* @param file
* the file
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static String readFirstLine(final File file) throws IOException
{
String result;
result = readFirstLine(new Files(file));
//
return result;
}
/**
* Read first line.
*
* @param files
* the file
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static String readFirstLine(final Files files) throws IOException
{
String result;
FilesLineIterator iterator = null;
try
{
iterator = new FilesLineIterator(files);
if (iterator.hasNext())
{
result = iterator.next();
}
else
{
result = "";
}
}
finally
{
if (iterator != null)
{
iterator.close();
}
}
//
return result;
}
/**
* Read first line not blank.
*
* @param files
* the files
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static String readFirstLineNotBlank(final Files files) throws IOException
{
String result;
FilesLineIterator iterator = null;
result = null;
try
{
iterator = new FilesLineIterator(files);
boolean ended = false;
while (!ended)
{
if (iterator.hasNext())
{
result = iterator.next();
if (!StringUtils.isBlank(result))
{
ended = true;
}
}
else
{
result = null;
}
}
}
finally
{
if (iterator != null)
{
iterator.close();
}
}
//
return result;
}
/**
* Search recursively.
*

View file

@ -37,6 +37,7 @@ public class LineIterator
{
private static Logger logger = LoggerFactory.getLogger(LineIterator.class);
private File file;
private BufferedReader in;
private String nextLine;
private boolean ready;
@ -51,6 +52,8 @@ public class LineIterator
*/
public LineIterator(final File source) throws IOException
{
this.file = source;
if (source.getName().endsWith(".gz"))
{
this.in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(source))));
@ -72,6 +75,16 @@ public class LineIterator
IOUtils.closeQuietly(this.in);
}
/**
* Gets the file.
*
* @return the file
*/
public File getFile()
{
return this.file;
}
/**
* Checks for next.
*
@ -130,42 +143,4 @@ public class LineIterator
this.ready = true;
}
}
/**
* Read first line.
*
* @param file
* the file
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static String readFirstLine(final File file) throws IOException
{
String result;
LineIterator iterator = null;
try
{
iterator = new LineIterator(file);
if (iterator.hasNext())
{
result = iterator.next();
}
else
{
result = "";
}
}
finally
{
if (iterator != null)
{
iterator.close();
}
}
//
return result;
}
}