/* * Class FileInput * * Methods for entering doubles, floats, BigDecimal, * integers, long integers, short integers, bytes, * booleans, Complexes, Phasors, lines (as String), * words (as String) and chars from a text file. * * WRITTEN BY: Dr Michael Thomas Flanagan * * DATE: July 2002 * REVISED: 25 July 2004, 11 June 2005, 13 September 2005, 30 November 2005, 1 July 2006 * 20 September 2006, 27 June 2007, 21-23 July 2007, 26 February 2008, 4 April 2008 * 7 July 2008, 8 July 2008, 2 February 2009, 13 November 2010, 13 December 2010, 20 May 2012 * * DOCUMENTATION: * See Michael Thomas Flanagan's Java library on-line web page: * http://www.ee.ucl.ac.uk/~mflanaga/java/FileInput.html * http://www.ee.ucl.ac.uk/~mflanaga/java/ * * Copyright (c) 2002 - 2012 Michael Thomas Flanagan * * PERMISSION TO COPY: * Permission to use, copy and modify this software and its documentation for * NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement * to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies. * * Dr Michael Thomas Flanagan makes no representations about the suitability * or fitness of the software for any or for a particular purpose. * Michael Thomas Flanagan shall not be liable for any damages suffered * as a result of using, modifying or distributing this software or its derivatives. * ***************************************************************************************/ package flanagan.io; import java.io.*; import java.math.*; import java.util.ArrayList; import flanagan.complex.Complex; import flanagan.circuits.Phasor; public class FileInput{ // Instance variables protected String fileName = " "; //input file name protected String stemName = " "; //input file name without its extension protected String extension = " "; //input file name extension protected String pathName = " "; //input file path name protected String dirPath = " "; //path to directory containing input file protected String fullLine = " "; //current line in input file protected String fullLineT = " "; //current line in input file trimmed of trailing spaces protected BufferedReader input = null; //instance of BufferedReader protected boolean testFullLine = false; //false if fullLine is empty protected boolean testFullLineT = false; //false if fullLineT is empty protected boolean eof = false; //true if reading beyond end of file attempted protected boolean fileFound = true; //true if file named is found protected boolean inputType = false; //false in input type is a String with inclusion of punctuation //true if input type is numeric or separated char, i.e. double, float, int, long, char protected boolean charType = false; //true if input type is a separated char protected boolean space = true; //if true - a space is treated as a delimiter in reading a line of text protected boolean supressMessage = false; //if true - read beyond end of file message suppressed protected boolean wordMethod = false; // true if readWord called protected String holdingWord = "HoldingWordMLPYGV"; // word used to hold an identified space that needs retaining on calling readWord // Constructor // Constructor to enable sub-class FileChooser to function public FileInput(){ } // constructor for instances of this class public FileInput(String pathName){ this.pathName = pathName; this.fileName = pathName; int posSlash = pathName.indexOf("//"); int posBackSlash = pathName.indexOf("\\"); if(posSlash!=-1 || posBackSlash!=-1){ File file = new File(this.pathName); this.fileName = file.getName(); this.dirPath = (file.getParentFile()).toString(); } int posDot = this.fileName.indexOf('.'); if(posDot==-1){ this.stemName = this.fileName; } else{ this.stemName = this.fileName.substring(0, posDot); this.extension = this.fileName.substring(posDot); } try{ this.input = new BufferedReader(new FileReader(this.pathName)); }catch(java.io.FileNotFoundException e){ System.out.println(e); fileFound=false; } } // Methods // Get file path public String getPathName(){ return this.pathName; } // Get file name public String getFileName(){ return this.fileName; } // get file name without the extension public String getStemName(){ return this.stemName; } // get file name extension public String getExtension(){ return this.extension; } // Get path to directory containing the file public String getDirPath(){ return this.dirPath; } // Removes a space to the list of delimiters on reading a line of text public void removeSpaceAsDelimiter(){ this.space = false; } // Restores a space from the list of delimiters on reading a line of text public void restoreSpaceAsDelimiter(){ this.space = true; } // Copy the file public final synchronized void copy(String copyFilename){ FileOutput fout = new FileOutput(copyFilename); int nLines = this.numberOfLines(); for(int i=0; i=0){ this.fullLineT=this.fullLineT.substring(0,i); i--; } } } // Check for space/s only or "" between non-space separators if method readWord called // substitute holding word if found public void checkWordSpaces(){ int len=this.fullLine.length(); // Check for space/s only or "" between non-space separators if method readWord called // substitute holding word if found if(this.wordMethod){ boolean test3 = false; ArrayList al1 = new ArrayList(); ArrayList al2 = new ArrayList(); for(int i=0; i0){ boolean test4 = true; boolean test5 = true; boolean test6 = false; int ii = 0; int jj = 0; int kk = 0; while(test4){ test5 = true; test6 = false; jj =((Integer)al1.get(ii)).intValue(); if(jj==0){ test6 = true; } else{ if(jj==len-1){ test6 = true; test4 = false; } else{ if(ii==nal1-1){ test5 = true; for(int i=jj+1; i=nal1)test4 = false; } int nal2 = al2.size(); if(nal2>0){ for(int i=nal2-1; i>=0; i--){ int mm = ((Integer)al2.get(i)).intValue(); if(mm>=len-1){ this.fullLine += this.holdingWord; } else{ if(mm==1){ this.fullLine = this.holdingWord + this.fullLine; } else{ String sub1 = this.fullLine.substring(0,mm); String sub2 = this.fullLine.substring(mm); this.fullLine = sub1 + this.holdingWord + sub2; } } } } } } } // reads the next word (a string between delimiters) from the String fullLine protected final synchronized String nextWord(){ this.testFullLine=true; this.testFullLineT=true; String word = ""; int posspa=-1, postab=-1, possp=-1, poscom=-1, poscol=-1, possem=-1; boolean test = true; int len=this.fullLine.length(); // strip end of the word of any spaces, tabs or, if numerical input, commas, colons or semicolons boolean test0 = true; boolean test1 = false; int pend =this.fullLine.length(); while(test0){ pend--; if(this.fullLine.charAt(pend)==' ')test1=true; if(this.fullLine.charAt(pend)=='\t')test1=true; if(inputType){ if(this.fullLine.charAt(pend)==',')test1=true; if(this.fullLine.charAt(pend)==':')test1=true; if(this.fullLine.charAt(pend)==';')test1=true; } if(test1){ this.fullLine = this.fullLine.substring(0,pend); } else{ test0=false; } test1=false; } // strip front of the word of any leading spaces, tabs or, if numerical input, commas, colons or semicolons test0 = true; test1 = false; while(test0){ if(this.fullLine.charAt(0)==' ')test1=true; if(this.fullLine.charAt(0)=='\t')test1=true; if(inputType){ if(this.fullLine.charAt(0)==',')test1=true; if(this.fullLine.charAt(0)==':')test1=true; if(this.fullLine.charAt(0)==';')test1=true; } if(test1){ this.fullLine = this.fullLine.substring(1); } else{ test0=false; } test1=false; } // find first space (if space allowed as delimiter), tab or, if numeric, comma, colon or semicolon int lenPlus = this.fullLine.length() + 10; if(this.space)posspa=this.fullLine.indexOf(' '); postab=this.fullLine.indexOf('\t'); int firstMin = lenPlus; int secondMin = lenPlus; int thirdMin = lenPlus; if(this.space){ if(posspa==-1 && postab==-1){ firstMin = lenPlus; } else{ if(posspa==-1){ firstMin = postab; } else{ if(postab==-1){ firstMin = posspa; } else{ firstMin = Math.min(posspa, postab); } } } } else{ if(postab!=-1){ firstMin = postab; } } if(this.inputType){ poscom=this.fullLine.indexOf(','); poscol=this.fullLine.indexOf(':'); possem=this.fullLine.indexOf(';'); if(poscom==-1 && poscol==-1){ secondMin = lenPlus; } else{ if(poscom==-1){ secondMin = poscol; } else{ if(poscol==-1){ secondMin = poscom; } else{ secondMin = Math.min(poscom, poscol); } } } if(possem==-1){ thirdMin = lenPlus; } else{ thirdMin = possem; } secondMin = Math.min(secondMin, thirdMin); firstMin = Math.min(firstMin, secondMin); } // remove first word first word from string if(firstMin==lenPlus){ word=this.fullLine; this.fullLine=""; this.testFullLine=false; } else{ word=this.fullLine.substring(0,firstMin); if(firstMin+1>this.fullLine.length()){ this.fullLine=""; this.testFullLine=false; } else{ this.fullLine=this.fullLine.substring(firstMin+1); if(this.fullLine.length()==0)this.testFullLine=false; } } if(this.testFullLineT){ if(!this.testFullLine){ this.testFullLineT=false; this.fullLineT=""; } else{ if(firstMin+1>this.fullLineT.length()){ this.fullLineT=""; this.testFullLineT=false; } } } // return first word of the supplied string return word; } // reads the next char from the String fullLine protected final synchronized char nextCharInString(){ this.testFullLine=true; char ch=' '; boolean test = true; ch=this.fullLine.charAt(0); this.fullLine=this.fullLine.substring(1); if(this.fullLine.length()==0)this.testFullLine=false; if(this.testFullLineT){ this.fullLineT=this.fullLineT.substring(1); if(this.fullLineT.length()==0)this.testFullLineT=false; } return ch; } // set supressMessage to true public void setSupressMessageToTrue(){ this.supressMessage = true; } // set supressMessage to false public void setSupressMessageToFalse(){ this.supressMessage = false; } // Returns the number of lines in the file public final synchronized int numberOfLines(){ FileInput fin = new FileInput(this.pathName); fin.setSupressMessageToTrue(); boolean test = true; int nLines = 0; while(test){ String inputLine = fin.readLineL(); if(fin.eof){ test = false; } else{ if(nLines==Integer.MAX_VALUE){ System.out.println("Class FileInput; method numberOfLines; The number of lines is greater than the maximum integer value, " + Integer.MAX_VALUE); System.out.println("-1 returned"); nLines = -1; } else{ nLines++; } } } fin.close(); return nLines; } // Delete a file public static void deleteFile(String fileName){ // deletion flag boolean flag = true; // Create an instance of File representing the named file File file0 = new File(fileName); // Check that the file exists if(!file0.exists()){ System.out.println("Method deleteFile: no file or directory of the name " + fileName + " found"); flag = false; } // Check whether FileName is write protected if(!file0.canWrite()){ System.out.println("Method deleteFile: " + fileName + " is write protected and cannot be deleted"); flag = false; } // Check, if fileName is a directory, that it is empty if(file0.isDirectory()){ String[] dirFiles = file0.list(); if (dirFiles.length > 0){ System.out.println("Method deleteFile: " + fileName + " is a directory which is not empty; no action was taken"); flag = false; } } // Delete fileName boolean deleteDone = file0.delete(); if (!deleteDone){ System.out.println("Method deleteFile: deletion of the file " + fileName + " failed"); } } }