package com.csc.library.utilities; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; public class ReadFile { private String fileName; private FileReader fin; private BufferedReader bin; private InputStreamReader isr; private FileInputStream fis; private String CharSet="UTF-8"; private boolean ready = true; public ReadFile(String fileName) { try { //Declare fileName as file fin = new FileReader(fileName); File file=new File(fileName); fis= new FileInputStream(file); isr = new InputStreamReader(fis,this.CharSet); isr = new InputStreamReader(fis); bin = new BufferedReader(isr); } catch (IOException e) { MyLog.error(this, e); } } public void setCharSet(String charset){ this.CharSet=charset; } 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) { MyLog.error(this, e); } catch (IOException e) { MyLog.error(this, e); } return null; } public int read() { try { return this.bin.read(); } catch (IOException e) { // TODO Auto-generated catch block MyLog.error(this, e); return -1; } } public int read(char[] cbuff) { try { return this.bin.read(cbuff); } catch (IOException e) { MyLog.error(this, e); return -1; } } public int read(char[] cbuff, int offs, int len) { try { return this.bin.read(cbuff, offs, len); } catch (IOException e) { MyLog.error(this, e); return -1; } } public String readLine() { try { return this.bin.readLine(); } catch (IOException e) { MyLog.error(this, e); return ""; } } public void setFileName(String fName) { this.fileName = fName; } public String getFileName() { return this.fileName; } private FileReader getFileReader() { return this.fin; } private FileInputStream getFileInputStream(){ return fis; } public boolean hasMoreData() { try { return bin.ready(); } catch (IOException ex) { return false; } } public String getString() { StringBuffer strBuff = new StringBuffer(); /* int data; char[] ch = new char[128]; data= this.read(ch); while (data!=-1) { strBuff.append(ch); data= this.read(ch); } */ try { while (bin.ready()) { strBuff.append((char) bin.read()); } return strBuff.toString(); } catch (Exception e) { MyLog.error(this, e); } return strBuff.toString(); } public StringBuffer getStringBuffer() { StringBuffer strBuff = new StringBuffer(); int data; char[] ch = new char[128]; data= this.read(ch); while (data!=-1) { strBuff.append(ch); data= this.read(ch); } /* try { while (bin.ready()) { strBuff.append((char) bin.read()); } return strBuff.toString(); } catch (Exception e) { MyLog.error(this, e); } */ return strBuff; } 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) { MyLog.error(this, ex); } 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) { MyLog.error(this, ex); } 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) { MyLog.error(this, ex); return null; } } public String getStringRL() { try { String str = ""; if ((str = bin.readLine()) != null) { return str; } } catch (IOException ex) { MyLog.error(this, ex); } return null; } public void resetFile() { try { fin = new FileReader(this.fileName); File file=new File(fileName); fis= new FileInputStream(file); isr = new InputStreamReader(fis,"UTF-8"); bin = new BufferedReader(fin); } catch (Exception ex) { MyLog.error(this, ex); } } public void closeFile() { try { this.bin.close(); this.fin.close(); fis.close(); isr.close(); } catch (IOException ex) { MyLog.error(this, ex); } } public String getCharSet() { return CharSet; } }