package data; /** * Angela Chen * Feb. 6, 2004 * * This class writes various information to text file. * */ import java.io.*; import java.util.*; public class DataIO { /** * writeToFile writes the User's name and description. * -used for Project2 */ public synchronized static void writeToFile( User user, String fileName ) throws IOException { PrintWriter out = new PrintWriter( new FileWriter( fileName, true ) ); out.println( "Name: " + user.getFirstName() + " " + user.getLastName() ); out.println( user.getDescription().trim() + "|" ); out.close(); } /** * writeCoordinates - used for testing purposes of passing parameters * from director shockwave movie to jsp. * - used for Final Project */ public synchronized static void writeCoordinates( String xCoord, String yCoord, String fileName ) throws IOException { PrintWriter out = new PrintWriter( new FileWriter( fileName, true ) ); out.println( "(" + xCoord + ", " + yCoord + ")" ); out.close(); } /** * writeUTM - takes in x and y coordinates, converts them to UTM coordinates * corresponding to maps of Del Mar, La Jolla, and Point Loma. Then * writes them to the file specified in the parameter. * - used for Final Project */ public synchronized static void writeUTM( String xCoord, String yCoord, String fileName) throws IOException { String easting, northing; int tempEast, tempNorth; // The minimum easting and northing coordinates for San Diego double minEasting = 476547; double minNorthing = 3609530; // Scale factors based on 800width x 450height director movie double xScale = 14.72; double yScale = 92.37; double x = Double.parseDouble( xCoord ); double y = Double.parseDouble( yCoord ); tempEast = (int) Math.round( (x * xScale) + minEasting ); tempNorth = (int) Math.round( (y * yScale) + minNorthing ); easting = Integer.toString( tempEast ); northing = Integer.toString( tempNorth ); PrintWriter out = new PrintWriter( new FileWriter( fileName, true ) ); out.println("Easting: " + easting); out.println("Northing: " + northing + "\n" ); out.close(); } /** * writeUserName - writes the user's first and last name to a file. * -used for final project. */ public synchronized static void writeUserName( String firstName, String lastName, String fileName ) throws IOException { PrintWriter out = new PrintWriter( new FileWriter( fileName, true ) ); out.println( "NAME: " + firstName + " " + lastName ); out.close(); } public static User getUserDescription( String firstName, String lastName, String fileName )throws IOException { BufferedReader in = new BufferedReader( new FileReader( fileName ) ); User user = new User(); String line = in.readLine(); String description = ""; String temp; StringTokenizer token = new StringTokenizer( line ); while( line != null ) { temp = token.nextToken(); if ( temp.equals("Name:") ){ temp = token.nextToken(); if ( temp.equalsIgnoreCase(firstName) ) { temp = token.nextToken(); if ( temp.equalsIgnoreCase(lastName) ) { line = in.readLine(); StringTokenizer t = new StringTokenizer( line, "|" ); description = t.nextToken(); user.setFirstName(firstName); user.setLastName(lastName); user.setDescription(description); } } } line = in.readLine(); } //end while in.close(); return user; } // end findDescription } // end DataIO