//================================================================================================== // S t a n d a r d Java // I n p u t T e x t F i l e // By Bruno Bachelet //================================================================================================== // Copyright (c) 1999-2016 // Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net // // This file is part of the B++ Library. This library is free software; you can redistribute it // and/or modify it under the terms of the GNU Library General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your option) any later // version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU Library General Public License for more details (http://www.gnu.org).
/*DESCRIPTION*/ /* This module provides a class to manipulate input text files. */
// Package //--------------------------------------------------------------------------------------- package bpp.standard;
// Importation //----------------------------------------------------------------------------------- import java.io.*;
// I n p u t T e x t F i l e Class //-------------------------------------------------------------- /*CLASS InputTextFile */ /* Represents an input text file. */ public class InputTextFile extends InputTextStream { //---------------------------------------------------------------------------------------Attributes protected BufferedReader atReader; protected StreamTokenizer atStream; //--------------------------------------------------------------------------------------Constructor /*METHOD InputTextFile */ /* Builds and opens an input text file. */ public InputTextFile(String agName) throws Exception { try { atReader=new BufferedReader(new FileReader(agName)); atStream=new StreamTokenizer(atReader); atStream.wordChars(33,255); }
catch (Exception e) { throw new Exception("Can't open the input file."); } } //---------------------------------------------------------------------------------------ReadString /*METHOD InputTextFile */ /* Reads a string from the file. */ public String readString() throws Exception { if (atStream.nextToken()==StreamTokenizer.TT_WORD) return (atStream.sval); return (""); } //-----------------------------------------------------------------------------------------ReadLine /*METHOD InputTextFile */ /* Reads a line from the file. */ public String readLine() throws Exception { String lcLine = "";
while (lcLine.equals("")) { if (isEnded()) return (""); lcLine=atReader.readLine(); }
return (lcLine); } //--------------------------------------------------------------------------------------ReadInteger /*METHOD InputTextFile */ /* Reads an integer from the file. */ public int readInteger() throws Exception { if (atStream.nextToken()==StreamTokenizer.TT_NUMBER) return ((int)atStream.nval); throw new Exception("Can't read an integer from the file."); } //-----------------------------------------------------------------------------------------ReadReal /*METHOD InputTextFile */ /* Reads a real number from the file. */ public double readReal() throws Exception { if (atStream.nextToken()==StreamTokenizer.TT_NUMBER) return (atStream.nval); throw new Exception("Can't read a real number from the file."); } //------------------------------------------------------------------------------------------IsEnded /*METHOD InputTextFile */ /* Indicates if the file is ended. */ public boolean isEnded() throws Exception { return (!atReader.ready()); } }
// End //------------------------------------------------------------------------------------------- |
|