Added check for utf-8 file
This commit is contained in:
parent
a372161dd5
commit
8b8e7ebab7
@ -1,6 +1,10 @@
|
|||||||
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 {
|
||||||
@ -18,43 +22,34 @@ public class fileProcessor {
|
|||||||
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();
|
reader.close();
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -63,12 +58,28 @@ public class fileProcessor {
|
|||||||
|
|
||||||
Set set = hashMap.entrySet();
|
Set set = hashMap.entrySet();
|
||||||
Iterator i = set.iterator();
|
Iterator i = set.iterator();
|
||||||
while (i.hasNext())
|
while (i.hasNext()) {
|
||||||
{
|
|
||||||
Map.Entry me = (Map.Entry) i.next();
|
Map.Entry me = (Map.Entry) i.next();
|
||||||
returnString = returnString.replaceAll(me.getKey().toString(), me.getValue().toString());
|
returnString = returnString.replaceAll(me.getKey().toString(), me.getValue().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnString;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user