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;
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;
}
}