Added new code to check binary file

This commit is contained in:
Keith Armstrong 2025-01-09 19:27:01 -05:00
parent f32ef9c56e
commit 33a8eaa4c5

View File

@ -9,15 +9,18 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.nio.charset.CodingErrorAction; import java.nio.charset.CodingErrorAction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class fileProcessor { public class fileProcessor {
public static String readFirstLine(String thisFile) throws IOException { public static String readFirstLine(String thisFile) throws IOException {
File file = new File(thisFile); File file = new File(thisFile);
Boolean isValid = validFile(thisFile); Boolean isValid = validFile(thisFile);
if ( !isValid ) if ( !isValid )
{ {
return ""; return null;
} }
if (file.exists()) { if (file.exists()) {
@ -28,7 +31,9 @@ public class fileProcessor {
return s; return s;
} }
} }
return null; return null;
} }
public static Boolean getFile(String myFile) { public static Boolean getFile(String myFile) {
@ -54,7 +59,9 @@ public class fileProcessor {
FileReader fileReader = new FileReader(myFile); FileReader fileReader = new FileReader(myFile);
BufferedReader reader = new BufferedReader(fileReader); BufferedReader reader = new BufferedReader(fileReader);
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null)
{
line = line.replaceAll("(?m)^\\s+|\\s+$", "");
sb.append(line); sb.append(line);
} }
reader.close(); reader.close();
@ -75,6 +82,33 @@ public class fileProcessor {
return returnString; return returnString;
} }
public static boolean fileHasText(String thisFile, String word) throws FileNotFoundException
{
boolean flag = false;
Scanner sc2 = new Scanner(new FileInputStream(thisFile));
while(sc2.hasNextLine()) {
String line = sc2.nextLine();
if( line.indexOf(word) != -1 )
{
flag = true;
}
}
return flag;
}
public static boolean stringHasText(String line, String regex) throws FileNotFoundException
{
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
int matches = 0;
while ( matcher.find() )
{
matches++;
}
return (matches > 0);
}
public static boolean validFile(String thisFile) public static boolean validFile(String thisFile)
{ {
Path path = Paths.get(thisFile); Path path = Paths.get(thisFile);