Added numberOfLines function

This commit is contained in:
Keith Armstrong 2025-01-04 08:31:45 -05:00
parent 96c0742b5e
commit 61f3968aaf

View File

@ -73,19 +73,41 @@ public class fileProcessor {
} }
public static Boolean validFile(String myFile) { public static Boolean validFile(String myFile) {
Path path = Paths.get(myFile); Path path = Paths.get(myFile);
try (Reader reader = Files.newBufferedReader(path)) { try (Reader reader = Files.newBufferedReader(path)) {
int c = reader.read(); int c = reader.read();
if (c == 0xfeff) { if (c == 0xfeff)
System.out.println("File starts with a byte order mark."); {
} else if (c >= 0) { /* System.out.println("File starts with a byte order mark."); */
}
else if (c >= 0)
{
reader.transferTo(Writer.nullWriter()); reader.transferTo(Writer.nullWriter());
} }
} catch (CharacterCodingException e) { } catch (CharacterCodingException e) {
return false; return false;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); return false;
// throw new RuntimeException(e);
} }
return true; return true;
} }
public static long numberOfLines(String fileName)
{
Path path = Paths.get(fileName);
long lines = 0;
try
{
lines = Files.lines(path).count();
}
catch (IOException e)
{
e.printStackTrace();
}
return lines;
}
} }