package com.csc.library.utilities;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ReadTXTFile {
	private  String fileName = "";
	private  FileReader fin;
	private  BufferedReader bin;
	private  int rows = 0;
	private  int cols = 0;
	private  String row_current = "";
	private  String col_value = "";
	private boolean ready = true;
	public ReadTXTFile(String fileName, String row, String col) {
		this.setAttribute(fileName, Integer.parseInt(row), Integer.parseInt(col));
	}
	public ReadTXTFile(String fileName, int row, int col) {
		this.setAttribute(fileName, row, col);
	}
	private void setAttribute(String fileName, int row, int col) {
		this.setFileName(fileName);
		this.setRow(row);
		this.setCol(col);
	}
	public void setFileName(String fName) {
		this.fileName = fName;
	}
	private String getFileName() {
		return this.fileName;
	}
	private FileReader getFileReader() {
		return this.fin;
	}
	public void setRow(int row) {
		this.rows = row;
	}
	public void setCol(int col) {
		this.cols = col;
	}
	private int getRow() {
		return this.rows;
	}
	private int getCol() {
		return this.cols;
	}
	private String getRowValue() {
		return this.row_current;
	}
	private String getColValue() {
		return this.col_value;
	}
	private void readFile() {
		try {
			//Declare fileName as file
			fin = new FileReader(fileName);
			bin = new BufferedReader(fin);
		} catch (IOException e) {
			MyLog.error(this, "FileStreamsTest: " + e);
		}
	}
	private void getLine() {
		int i;
		try {
			for (i = 0; i < this.rows && this.hasMoreData(); i++) {
				bin.readLine();
			}
			if (i == this.rows && this.hasMoreData()) {
				row_current = bin.readLine();
			}
		} catch (IOException ex) {
			MyLog.error(this, ex);
		}
	}
	private void getColumns() {
		String[] columns =
			{
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"" };
		try {
			if (row_current != null) {
				StringTokenizer tz = new StringTokenizer(row_current, ",");
				int i = 0;
				while (tz.hasMoreTokens()) {
					columns[i++] = tz.nextToken();
				}
				//columns = row_current.split(",");
				if (columns.length > this.cols) {
					col_value = columns[this.cols];
				}
			}
		} catch (Exception ex) {
			MyLog.error(this, ex);
		}
	}
	public String getString() {
		this.readData();
		return this.getColValue();
	}
	private void readData() {
		this.readFile();
		this.getLine();
		this.getColumns();
		this.closeFile();
	}
	private boolean hasMoreData() {
		try {
			return bin.ready();
		} catch (IOException ex) {
			return false;
		}
	}
	private void resetFile() {
		try {
			fin = new FileReader(this.fileName);
			bin = new BufferedReader(fin);
		} catch (Exception ex) {
			MyLog.error(this, ex);
		}
	}
	private void closeFile() {
		try {
			this.bin.close();
			this.fin.close();
		} catch (IOException ex) {
			MyLog.error(this, ex);
		}
	}
	/*public static void main(String args[]){
	ReadTXTFile rt = new ReadTXTFile("c:/test.txt",0,0);
	System.out.println("data 0 : " + rt.getString());
	rt.setRow(0);
	rt.setCol(1);
	System.out.println("data 1 : " + rt.getString());
	rt.setRow(0);
	rt.setCol(2);
	System.out.println("data 2 : " + rt.getString());
	}*/
	/*public String getStringWithEnter() {
	      String tmp;
	      StringBuffer strBuff = new StringBuffer();
	      try {
	              while ((tmp = bin.readLine()) != null) {
	                      tmp += "" + (char) 13 + (char) 10;
	                      strBuff.append(tmp);
	              }
	              bin.close();
	              return strBuff.toString();
	      } catch (FileNotFoundException e) {
	              System.err.println("FileStreamsTest: " + e);
	      } catch (IOException e) {
	              System.err.println("FileStreamsTest: " + e);
	      }
	      return null;
	}
	
	
	
	public String getString() {
	      StringBuffer strBuff = new StringBuffer();
	      try {
	              while (bin.ready()) {
	                      strBuff.append((char) bin.read());
	              }
	              return strBuff.toString();
	      } catch (Exception e) {
	              e.printStackTrace();
	      }
	      return strBuff.toString();
	}
	
	public String getStringToken(char delim) {
	      try {
	              int count = 0;
	              StringBuffer strBuff = new StringBuffer();
	              char ch;
	              while (bin.ready()) {
	                      ch = (char) bin.read();
	                      if (ch == delim) {
	                              return strBuff.toString();
	                      } else {
	                              strBuff.append(ch);
	                      }
	              }
	              if (!bin.ready()) {
	                      return strBuff.toString();
	              } else
	                      this.ready = false;
	      } catch (IOException ex) {
	              ex.printStackTrace();
	      }
	      return "";
	}
	
	  public String getStringToken(int delim) {
	      try {
	              int count = 0;
	              StringBuffer strBuff = new StringBuffer();
	              int i;
	              while (bin.ready()) {
	                      i = bin.read();
	                      if (i == delim) {
	                              return strBuff.toString();
	                      } else {
	                              strBuff.append((char) i);
	                      }
	              }
	              if (!bin.ready()) {
	                      return strBuff.toString();
	              }
	      } catch (IOException ex) {
	              ex.printStackTrace();
	      }
	      return "";
	}
	
	  public String getCountString(int num) {
	      try {
	              StringBuffer strBuff = new StringBuffer();
	              for (int i = 0; i < num; i++) {
	                      if (bin.ready())
	                              strBuff.append((char) bin.read());
	              }
	              return strBuff.toString();
	      } catch (IOException ex) {
	              return null;
	      }
	}
	public String getStringRL() {
	      try {
	              char ch;
	              String str = "";
	              int i = 0;
	              if ((str = bin.readLine()) != null) {
	                      return str;
	              }
	      } catch (IOException ex) {
	              ex.printStackTrace();
	      }
	      return null;
	}
	*/
}