class="File Read Interface">package cn.newtouch.service; /** * The class FileReader will deal with all accesses to a file * * @author e397930 */ public interface SfjFileReaderServiceBatch { /** * Instantiates a new FileReader * * @param fileName * fileName * @throws SfjBatchTechnicalException * SfjBatchTechnicalException */ void open(String fileName); /** * Read a line of the file and return it as a string * * @return a line */ String readLine(); /** * Close the file. * * @throws SfjBatchTechnicalException * SfjBatchTechnicalException */ void close(); /** * Read the last line of file. * * @return last line */ String readEndLine(); /** * Getter lineCount * * @return the lineCount */ int getLineCount(); }
?
package cn.newtouch.service.impl; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.Scanner; import cn.newtouch.service.SfjFileReaderServiceBatch; /** * The class FileReader will deal with all accesses to a file * */ public class SfjFileReaderServiceBatchImpl implements SfjFileReaderServiceBatch { /** The file in channel. */ private FileChannel fileInChannel; /** The scannner. */ private Scanner scannner; /** The file name. */ @SuppressWarnings("unused") private String fileName; /** The line count. */ private int lineCount; /** * * {@inheritDoc} * * @see com.inetpsa.sfj.servicebatch.fileio.SfjFileReaderServiceBatch#open(java.lang.String) */ public void open(String fileName) { this.fileName = fileName; try { FileInputStream fin = new FileInputStream(fileName); fileInChannel = fin.getChannel(); scannner = new Scanner(fin); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * * {@inheritDoc} * * @see com.inetpsa.sfj.servicebatch.fileio.SfjFileReaderServiceBatch#readLine() */ public String readLine() { String line = null; if (scannner != null && scannner.hasNextLine()) { line = scannner.nextLine(); } return line; } /** * * {@inheritDoc} * * @see com.inetpsa.sfj.servicebatch.fileio.SfjFileReaderServiceBatch#close() */ public void close() { scannner.close(); try { fileInChannel.close(); } catch (IOException e) { e.printStackTrace(); } } /** * * {@inheritDoc} * * @see com.inetpsa.sfj.servicebatch.fileio.SfjFileReaderServiceBatch#readEndLine() */ public String readEndLine() { String line = null; if (scannner != null) { while (scannner.hasNextLine()) { line = scannner.nextLine(); lineCount++; } } return line; } /** * Getter lineCount * * @return the lineCount */ public int getLineCount() { return lineCount; } /** * Setter lineCount * * @param lineCount * the lineCount to set */ public void setLineCount(int lineCount) { this.lineCount = lineCount; } }
?