Presentation is loading. Please wait.

Presentation is loading. Please wait.

StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols,

Similar presentations


Presentation on theme: "StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols,"— Presentation transcript:

1 StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols, etc…) nextToken() returns the next token in the stream

2 Constants: end-of-file, end-of-line, parsed floating point number, and parsed word (whitespace) pushBack(): pushes the token back onto the stream. Can specify how tokens are recognized Reader r = new BufferedReader(new InputStreamReader(is)); StreamTokenizer st = new StreamTokenizer(r); Works very much like StringTokenizer

3 StringTokenizer(String str, String delim); str: Input string to tokenize delim: String containing delimitingcharacters. Default is “\t\n\r” Alternative for Project 2

4 Random Access Files java.io.RandomAccessFile Read arbitrary bytes, text, and primitive data types from or to any specified location in a file. new RandomAccessFile("farrago.txt", "rw"); seek(long pos): Used to select the position in the file ( pos )

5 skipBytes Moves the file pointer forward the specified number of bytes. seek Positions the file pointer just before the specified byte. getFilePointer Returns the current byte location of the file pointer.

6 URL Uniform Resource Locator and Java Class Protocol Identifier and resource protocol://resource URL gamelan = new URL("http://www.gamelan.com/"); Relative URL’s URL(URL baseURL, String relativeURL)

7 URL gamelan = new URL("http://www.gamelan.com/pages/"); URL gamelanGames = new URL(gamelan, "Gamelan.game.html"); URL gamelanNetwork = new URL(gamelan, "Gamelan.net.html"); URL gamelanNetworkBottom = new URL(gamelanNetwork, "#BOTTOM"); URL gamelan = new URL("http", "www.gamelan.com", 80, "pages/Gamelan.network.html"); try { URL myURL = new URL(...) } catch (MalformedURLException e) {... // exception handler code here... }

8 Parsing an URL getProtocol Returns the protocol identifier component of the URL. getHost Returns the host name component of the URL. getPort Returns the port number component of the URL. The getPort method returns an integer that is the port number. If the port is not set, getPort returns -1. getFile Returns the filename component of the URL. getRef Returns the reference component of the URL.

9 public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://java.sun.com:80/docs/ books/tutorial/intro.html#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("host = " + aURL.getHost()); System.out.println("filename = " + aURL.getFile()); System.out.println("port = " + aURL.getPort()); System.out.println("ref = " + aURL.getRef()); } protocol = http host = java.sun.com filename = /docs/books/tutorial/intro.html port = 80 ref = DOWNLOADING

10 import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.yahoo.com/"); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } } import java.net.*; import java.io.*;

11 import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.yahoo.com/"); URLConnection yc = yahoo.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }

12 import java.io.*; import java.net.*; public class Reverse { public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: java Reverse string_to_reverse"); System.exit(1); } String stringToReverse = URLEncoder.encode(args[0]); //encode spaces and nonalphanumerics for network processing URL url = new URL("http://java.sun.com/cgi-bin/backwards"); URLConnection connection = url.openConnection(); //will we have an output to URL connection connection.setDoOutput(true);

13 PrintWriter out = new PrintWriter(connection.getOutputStream()); out.println("string=" + stringToReverse); out.close(); BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }


Download ppt "StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols,"

Similar presentations


Ads by Google