FileDocCategorySizeDatePackage
GraphReader.javaAPI DocExample1760Thu May 27 22:01:30 BST 2004None

GraphReader

public class GraphReader extends Object
GraphReader is a helper class for Grapher, that reads various formats. (At present, only a list of x,y points from a text file).

Fields Summary
Constructors Summary
Methods Summary
public static java.util.Listread(java.lang.String fileName)
Read data points from the named file. Each line has an x and a y coordinate.

throws
IllegalArgumentException on bad or insufficient data
throws
IOException if the file doesn't exist or is unreadable.

		return read(new FileReader(fileName), fileName);
	
public static java.util.Listread(java.io.Reader reader, java.lang.String fileName)
Read data points from an opened Reader. Each line has an x and a y coordinate.

throws
IllegalArgumentException on bad or insufficient data
throws
IOException if the file doesn't exist or is unreadable.

		LineNumberReader is = new LineNumberReader(reader);

		List data = new ArrayList();

		String txt;
	
		// Read the file a line at a time, parse it, save the data.
		while ((txt = is.readLine()) != null) {
			StringTokenizer st = new StringTokenizer(txt);
			try {
				Point2D p = new Point2D.Double();
				p.setLocation(Float.parseFloat(st.nextToken()),
					Float.parseFloat(st.nextToken()));
				data.add(p);
			} catch(NumberFormatException nfe) {
				throw new IllegalArgumentException("Invalid number " + nfe + 
						" on line " + is.getLineNumber());
			}
		}

		if (data.size() < 2) {
			throw new IllegalArgumentException(
				"GraphReader.read: " + fileName + ": Not enough data points!");
		}
		return data;