From 8b8e7ebab75a06f2b87f7b2149535a673d92914b Mon Sep 17 00:00:00 2001 From: Keith Armstrong Date: Thu, 2 Jan 2025 16:58:50 -0500 Subject: [PATCH] Added check for utf-8 file --- src/com/belkast/soap/fileProcessor.java | 101 +++++++++++++----------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/src/com/belkast/soap/fileProcessor.java b/src/com/belkast/soap/fileProcessor.java index 6f81cb6..58e4057 100644 --- a/src/com/belkast/soap/fileProcessor.java +++ b/src/com/belkast/soap/fileProcessor.java @@ -1,74 +1,85 @@ package com.belkast.soap; import java.io.*; +import java.nio.charset.CharacterCodingException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.*; public class fileProcessor { public static String readFirstLine(String thisFile) throws IOException { - File file = new File(thisFile); - if (file.exists()) { - FileReader fr = new FileReader(file); - LineNumberReader ln = new LineNumberReader(fr); - while (ln.getLineNumber() == 0) { - String s = ln.readLine(); - return s; - } + File file = new File(thisFile); + if (file.exists()) { + FileReader fr = new FileReader(file); + LineNumberReader ln = new LineNumberReader(fr); + while (ln.getLineNumber() == 0) { + String s = ln.readLine(); + return s; } + } return null; } - public static Boolean getFile (String myFile) - { - if (myFile == null) - { + public static Boolean getFile(String myFile) { + if (myFile == null) { return false; } File thisFile = new File(myFile); - if (thisFile.exists()) - { + if (thisFile.exists()) { return true; } return false; } - public static String replaceInFile (String myFile, Map hashMap) - { + public static String replaceInFile(String myFile, Map hashMap) { File thisFile = new File(myFile); - if (!thisFile.exists()) - { + if (!thisFile.exists()) { return null; } StringBuilder sb = new StringBuilder(); - try - { - FileReader fileReader = new FileReader(myFile); - BufferedReader reader = new BufferedReader(fileReader); - String line; - while ((line = reader.readLine()) != null) - { - sb.append(line); - } - reader.close(); + try { + FileReader fileReader = new FileReader(myFile); + BufferedReader reader = new BufferedReader(fileReader); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); } - catch (IOException e) - { - e.printStackTrace(); - return null; - } - - String returnString = sb.toString(); - - Set set = hashMap.entrySet(); - Iterator i = set.iterator(); - while (i.hasNext()) - { - Map.Entry me = (Map.Entry)i.next(); - returnString = returnString.replaceAll(me.getKey().toString(), me.getValue().toString()); - } - - return returnString; + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + return null; } + + String returnString = sb.toString(); + + Set set = hashMap.entrySet(); + Iterator i = set.iterator(); + while (i.hasNext()) { + Map.Entry me = (Map.Entry) i.next(); + returnString = returnString.replaceAll(me.getKey().toString(), me.getValue().toString()); + } + + return returnString; + } + + public static Boolean validFile(String myFile) { + Path path = Paths.get(myFile); + try (Reader reader = Files.newBufferedReader(path)) { + int c = reader.read(); + if (c == 0xfeff) { + System.out.println("File starts with a byte order mark."); + } else if (c >= 0) { + reader.transferTo(Writer.nullWriter()); + } + } catch (CharacterCodingException e) { + return false; + } catch (IOException e) { + throw new RuntimeException(e); + } + return true; + } } \ No newline at end of file