Added check for utf-8 file

This commit is contained in:
Keith Armstrong 2025-01-02 16:58:50 -05:00
parent a372161dd5
commit 8b8e7ebab7

View File

@ -1,74 +1,85 @@
package com.belkast.soap; package com.belkast.soap;
import java.io.*; 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.*; import java.util.*;
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);
if (file.exists()) { if (file.exists()) {
FileReader fr = new FileReader(file); FileReader fr = new FileReader(file);
LineNumberReader ln = new LineNumberReader(fr); LineNumberReader ln = new LineNumberReader(fr);
while (ln.getLineNumber() == 0) { while (ln.getLineNumber() == 0) {
String s = ln.readLine(); String s = ln.readLine();
return s; return s;
}
} }
}
return null; return null;
} }
public static Boolean getFile (String myFile) public static Boolean getFile(String myFile) {
{ if (myFile == null) {
if (myFile == null)
{
return false; return false;
} }
File thisFile = new File(myFile); File thisFile = new File(myFile);
if (thisFile.exists()) if (thisFile.exists()) {
{
return true; return true;
} }
return false; return false;
} }
public static String replaceInFile (String myFile, Map hashMap) public static String replaceInFile(String myFile, Map hashMap) {
{
File thisFile = new File(myFile); File thisFile = new File(myFile);
if (!thisFile.exists()) if (!thisFile.exists()) {
{
return null; return null;
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try try {
{ 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) sb.append(line);
{
sb.append(line);
}
reader.close();
} }
catch (IOException e) reader.close();
{ } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return null; 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;
} }
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;
}
} }