java file read_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java file read

java file read

 2015/1/7 12:41:59  lihongtai  程序员俱乐部  我要评论(0)
  • 摘要:packagecn.newtouch.service;/***TheclassFileReaderwilldealwithallaccessestoafile**@authore397930*/publicinterfaceSfjFileReaderServiceBatch
  • 标签:file Java
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;
	}

}

?

上一篇: 程序员需要学习代码编译器知识的因素 下一篇: 没有下一篇了!
发表评论
用户名: 匿名