Refactored the validFile method
Method is now Java 1.8 compatible
This commit is contained in:
parent
712ddc9421
commit
7ea1f88238
@ -2,10 +2,13 @@ package com.belkast.soap;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
|
||||
public class fileProcessor {
|
||||
|
||||
@ -72,26 +75,38 @@ public class fileProcessor {
|
||||
return returnString;
|
||||
}
|
||||
|
||||
public static Boolean validFile(String myFile) {
|
||||
public static boolean validFile(String thisFile)
|
||||
{
|
||||
Path path = Paths.get(thisFile);
|
||||
boolean result = true;
|
||||
long total = 0;
|
||||
InputStreamReader isr = null;
|
||||
char[] buf = new char[65536];
|
||||
int chars;
|
||||
|
||||
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) {
|
||||
return false;
|
||||
// throw new RuntimeException(e);
|
||||
final CharsetDecoder decoder;
|
||||
String charsetName = "UTF-8";
|
||||
Charset charset = Charset.forName(charsetName);
|
||||
decoder = charset.newDecoder();
|
||||
decoder.onMalformedInput(CodingErrorAction.REPORT);
|
||||
|
||||
try {
|
||||
isr = new InputStreamReader(new FileInputStream(thisFile), decoder);
|
||||
while ((chars = isr.read(buf)) >= 0)
|
||||
total += chars;
|
||||
} catch (CharacterCodingException ex) {
|
||||
result = false;
|
||||
} catch (IOException ex) {
|
||||
result = false;
|
||||
} finally {
|
||||
if (isr != null)
|
||||
try {
|
||||
isr.close();
|
||||
} catch (IOException ex) {
|
||||
System.err.println("Failed to close file: " + ex.toString());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long numberOfLines(String fileName)
|
||||
|
Loading…
x
Reference in New Issue
Block a user