/**
*
*/
package systole.ioHandling.fileReaders;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import systole.domain.signals.Segment;
import systole.exceptions.ExceptionIO;
import systole.ioHandling.logs.SystoleLogger;
import systole.math.MathUtils;
/**
* @author juan
*
*/
public class FileReaderTXT extends FileReaderSystole {
private FileReader fileReader;
/**
* Empty constructor.
*/
public FileReaderTXT() {
super();
this.fileReader = null;
}
/* (non-Javadoc)
* @see systole.ioHandling.fileReaders.FileReaderSystole#readFile()
*/
@Override
public Segment readFile(File file) throws ExceptionIO {
if (file == null) {
throw new ExceptionIO("Archivo inálido");
}
Segment points = new Segment();
String cadena = "";
BigDecimal value;
int currentLine = 1;
try {
this.fileReader = new FileReader(file);
BufferedReader bf = new BufferedReader(this.fileReader);
while ((cadena = (bf.readLine())) != null) {
value = new BigDecimal(new Double(cadena),MathUtils.CONTEXT);
points.add(value);
currentLine++;
}
this.fileReader.close();
return points;
} catch (IOException e) {
SystoleLogger.getInstance().logInfo("Error on read file, Msg: " + e.getMessage());
throw new ExceptionIO("No se pudo leer el archivo");
} catch (NumberFormatException n) {
SystoleLogger.getInstance().logInfo("Error on read line: " + cadena + " Msg: " + n.getMessage());
try {
this.fileReader.close();
} catch (IOException e) {
SystoleLogger.getInstance().logInfo("Error on close file, msg: " + e.getMessage());
}
throw new ExceptionIO("Error al leer la línea " + currentLine);
}
}
@Override
public Segment readFile(String path) throws ExceptionIO {
if (path == null) {
throw new ExceptionIO("Path inválido");
}
Segment points = new Segment();
String cadena = "";
BigDecimal value;
int currentLine = 1;
try {
this.fileReader = new FileReader(path);
BufferedReader bf = new BufferedReader(this.fileReader);
while ((cadena = (bf.readLine())) != null) {
value = new BigDecimal(new Double(cadena),MathUtils.CONTEXT);
points.add(value);
currentLine++;
}
this.fileReader.close();
return points;
} catch (IOException e) {
SystoleLogger.getInstance().logInfo("Error on read file: " + path + " Msg: " + e.getMessage());
throw new ExceptionIO("No se pudo leer el archivo");
} catch (NumberFormatException n) {
SystoleLogger.getInstance().logInfo("Error on read line: " + cadena + " Msg: " + n.getMessage());
try {
this.fileReader.close();
} catch (IOException e) {
SystoleLogger.getInstance().logInfo("Error on close file, msg " + e.getMessage());
}
throw new ExceptionIO("Error al leer la línea " + currentLine);
}
}
}