lecture6

lecture6 - Web Technology for Industrial Engineering 2...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Web Technology for Industrial Engineering 2 October 2003 http://www-classes.usc.edu/engr/ise/582 1 ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems Engineering Lecture 6 JAVA Cup Five: Data Structures 2 October 2003 Web Technology for IE 2 JAVA Cup 5 • File Access – Input file streams – Output file streams • Arrays and Vectors – How to create and access arrays – Expandable vectors • Characters and Strings 2 October 2003 Web Technology for IE 3 File Input Streams • Reading files one-byte-at-a-time • Taking bigger bites • Java’s input-output package • Traditional string handling • Updating to tokens: number / words
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Web Technology for Industrial Engineering 2 October 2003 http://www-classes.usc.edu/engr/ise/582 2 2 October 2003 Web Technology for IE 4 File input streams • A stream is a sequence of values. • To read bytes from a file: – FileInputStream stream = new FileInputStream(“ input.data ”) – FileInputStream stream = new FileInputStream(“~ username/public_html/ise582/input.dat a ”); • When you are done, it is good to: – stream.close() • Reads ONE BYTE AT A TIME… 2 October 2003 Web Technology for IE 5 To take bigger bites than bytes • To read characters from your file: – InputStreamReader reader = new InputStreamReader(stream); • To read lines from your file: – BufferedReader buffer = new BufferedReader(reader); – buffer.readLine() stream reader buffer 2 October 2003 Web Technology for IE 6 Input-output package • Notify JAVA that you want to work with input or output streams: – import java.io.FileInputStream – import java.io.* • In the event of error – use try-catch statements – throw an exception, throws IOException
Background image of page 2
Web Technology for Industrial Engineering 2 October 2003 http://www-classes.usc.edu/engr/ise/582 3 2 October 2003 Web Technology for IE 7 Traditional Approach Example import java.io.*; public class Demonstrate { public static void main(String argv[]) throws IOException { FileInputStream stream = new FileInputStream(“input.data”); InputStreamReader reader = new InputStreamReader(stream); BufferedReader buffer = new BufferedReader(reader); String line; System.out.println(“Line read: “ + line); } stream.close(); return; }} 2 October 2003 Web Technology for IE 8 String Methods • line.trim() – removes white space • line.indexOf(“ “) – index of first occurrence, starts from 0 • line.substring(2) – returns rest of line after index 2 • line.substring(0,1) • Integer.parseInt(“4”) – converts string to integer 2 October 2003 Web Technology for IE 9 Example Continued line = line.trim(); int nextSpace = line.indexOf(" "); int x = Integer.parseInt(line.substring(0,nextSpace)); line = line.substring(nextSpace).trim(); nextSpace=line.indexOf(" ");
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

lecture6 - Web Technology for Industrial Engineering 2...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online