/** * TextFileReader is a bean that provides the basic functionality for * reading a textfile. */ public class TextFileReader {
private String fileName, errorMessage; private int columns, rowCount;
/** * Constructs a TextFileReader. */ public TextFileReader() { reset(); }
/** * Resets all the variables in this bean. */ public void reset() { fileName = ""; errorMessage = ""; columns = 0; rowCount = 0; }
/** * Sets the error message, if an error occurs. */ public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
/** * Returns the error message, if any. */ public String getErrorMessage() { return errorMessage; }
/** * Returns the filename. */ public String getFileName() { return fileName; }
/** * Sets the filename. */ public void setFileName(String fileName) { this.fileName = fileName; }
/** * Returns the amount of rows in the file. */ public int getRows() { return rowCount; }
/** * Returns the maximum amount of columns in a row. */ public int getColumns() { return columns; }
/** * Returns the content of the file in a String. * If an error occurs, like if the file does not exists, null is returned. */ public String getContent() { String content = ""; File file = new File(fileName); if (!file.exists()) { setErrorMessage("Error: The file '" + fileName + "' does not exists."); return null; } else if (file != null) { try { // Create an BufferedReader so we can read a line at the time. BufferedReader reader = new BufferedReader(new FileReader(file)); String inLine = reader.readLine(); while (inLine != null) { if (inLine.length() + 1 > columns) columns = inLine.length() + 1; content += (inLine + System.getProperty("line.separator")); inLine = reader.readLine(); rowCount++; } return content; } catch (IOException e) { setErrorMessage("Error reading the file: " + e.getMessage()); return null; } } else { setErrorMessage("Unknown error!"); return null; } } }