Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
import java.io.*; import java.net.*; /** * A class to read values from a file, URL or stream * * @version 1.0 21.1.2000 * @author Anthony Steed */ public class SceneReader { /* * Private variables */ private BufferedReader stream; private StreamTokenizer reader; private boolean eof=false; /** * Create a SceneReader from a File * @param f the file to read from * @throw java.lang.Exception * on various file access errors */ SceneReader(File f) throws Exception { stream = new BufferedReader(new FileReader(f)); reader = new StreamTokenizer(stream); initCommon(); } /** * Create a SceneReader from a URL * @param u the url specifying the file location * @throw java.lang.Exception * on various URL access errors */ SceneReader(URL u) throws Exception { stream = new BufferedReader(new InputStreamReader(u.openStream())); reader = new StreamTokenizer(stream); initCommon(); } /** * Create a SceneReader from a stream * @param is the stream specifying the file location * @throw java.lang.Exception * on various stream access errors */ SceneReader(InputStream is) throws Exception { stream = new BufferedReader(new InputStreamReader(is)); reader = new StreamTokenizer(stream); initCommon(); } /* * Configure the tokenizer */ private void initCommon() { reader.commentChar('#'); reader.parseNumbers(); } /** * Close the file when finished * @throw java.io.IOException * on various access errors */ public void close() throws IOException { stream.close() ; } /** * Return true if the end of file has been reached. * @return boolean value */ public boolean eof() { return eof ; } /** * Read an int value from file. * @throw java.io.IOException * if source is unreadable * @throw java.lang.NumberFormatException * if number formating error */ public final synchronized int readInt() throws IOException, NumberFormatException { Double input; int val = 0 ; // Read next word via tokeniser input = nextNumber(); // Convert to a int val = input.intValue(); return val ; } /** * Read a long value from file. * @return a long value * @throw java.io.IOException * if source is unreadable * @throw java.lang.NumberFormatException * if number formating error */ public final synchronized long readLong() throws IOException, NumberFormatException { Double input; long val = 0L ; // Read next word via tokeniser input = nextNumber(); // Parse the String to construct a long value. val = input.longValue(); return val ; } /** * Read a double value from file. * @return a double value * @throw java.io.IOException * if source is unreadable * @throw java.lang.NumberFormatException * if number formating error */ public final synchronized double readDouble() throws IOException, NumberFormatException { Double input; double val = 0.0D ; // Read next word via tokeniser input = nextNumber(); // Convert to a double val = input.doubleValue(); return val ; } /** * Read a float value from file. * @return a float value * @throw java.io.IOException * if source is unreadable * @throw java.lang.NumberFormatException * if number formating error */ public final synchronized float readFloat() throws IOException, NumberFormatException { Double input; float val = 0.0F ; // Read next word via tokeniser input = nextNumber(); // Convert to a long val = input.longValue(); return val ; } /** * Read a char value from file. * @return a char value * @throw java.io.IOException * if source is unreadable */ public final synchronized char readChar() throws IOException { char c = ' '; // No need to parse anything, just get a character and return // it.. int n = stream.read() ; if (n == -1) { eof = true ; } else { c = (char)n ; } return c ; } /** * Read an String value from file. * @return a string value * @throw java.io.IOException * if source is unreadable */ public final synchronized String readString() throws IOException { // No need to parse anything, just get a string and return // it.. String s = "" ; s = stream.readLine() ; if (s == null) { eof = true ; s = ""; ; } return s ; } /* * Helper function to access tokenizer */ private Double nextNumber() throws IOException, NumberFormatException { double input; int tokenVal; // // Read a String of characters from the file. // tokenVal = reader.nextToken(); switch(tokenVal) { case StreamTokenizer.TT_NUMBER: input = reader.nval; break; case StreamTokenizer.TT_EOL: eof=true; throw new IOException("End of file"); default: throw new NumberFormatException("Next token was not a number " + reader.toString()); } return new Double(input); } }