// purpose: provides simpler file (local/web) manipulation import java.util.*; import java.io.*; import java.net.*; public class Connection { String pathName; // Connection(): creates a connection associated with the file named by name // in the folder named by folder public Connection( String folder, String name ) { // the complete path name is the concatenation of the folder & name this.pathName = folder + name; } // getName(): returns the path name for the connection public String getName() { return this.pathName; } // toString(): returns a string representation of the path name public String toString() { String s = this.getName(); return s; } // getScanner(): returns a new Scanner for reading from the file public Scanner getScanner() { // get the name of the file String s = this.getName(); // get a new stream -- need to take into account that the file is // either local or on the web Scanner stream; try { if ( s.indexOf( "http" ) == 0 ) { // its a web file to be read URL u = new URL( s ); stream = new Scanner( u.openStream() ); } else { // its a local file to be read File f = new File( s ); if ( f.exists() ) { stream = new Scanner( f ); } else { stream = null; } } } catch ( Exception e ) { stream = null; } // hand over the new scanner return stream; } public PrintStream getWriter() { // get the name of the file String s = this.getName(); try { // get a File representation of the file File f = new File( s ); // get a writer object that displays to the file PrintStream writer = new PrintStream( s ); // hand over the print stream return writer; } catch ( Exception e ) { return null; } } public void delete() { // get the name of the file String s = this.getName(); // get a File representation of the file File f = new File( s ); // throw the file away f.delete(); } // main(): simpler tester of some connection capabilities public static void main( String[] args ) throws IOException { Connection c = new Connection( "", "junk.txt" ); PrintStream out = c.getWriter(); out.println( "testing 1 2 3" ); out.println( "1 2 3 testing" ); Scanner in = c.getScanner(); while ( in.hasNext() ) { System.out.println( in.next() ) ; } c.delete(); } }