Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf ·...

11
Chapter 8 I/O Streams Monday, November 5, 12

Transcript of Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf ·...

Page 1: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Chapter 8

I/O Streams

Monday, November 5, 12

Page 2: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

How to read data from keyboard

• You have to use I/O Stream classes• For example, System.in is one of the

stream classes

I/OData Stream (Read) Java Program

Data Stream (Write)

Monday, November 5, 12

Page 3: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Stream types

• Binary Stream– InputStream– OutputStream

• Character Stream– Reader– Writer

Monday, November 5, 12

Page 4: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

InputStream

• public abstract int read() throws

IOException – เป็น Abstract Method ซึ่งส่งค่ากลับเป็นค่า int ที่อ่านได้มีค่าระหว่าง 0 ถึง 255 ในกรณีที่อ่านถึงตําแหน่งสุดท้ายของ Stream หรือมีการปิด Stream จะส่งค่ากลับเป็น -1 ถ้าเกิดความผิดพลาดขึ้น IOException จะถูกส่งออกมา

üpublic int read(byte[] b) throws IOException

Monday, November 5, 12

Page 5: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Example of InputStream (System.in)1. import java.io.*;2. class ReadFromKeyboard {3. public static void main(String[] args) 4. {5. byte[] input = new byte[100];6. System.out.print("Please enter your name here: ");7. try{8. System.in.read(input);9. System.out.println("Your name is " + new

String(input) );10. }11. catch (IOException e){12. System.out.println( e.getMessage() );13. }14. }15.}16.---------- Java Output----------17.Please enter your name here: Thirapon18.Your name is Thirapon

Open API

Can you print input with println?

Monday, November 5, 12

Page 6: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

การใช้ BufferedInputStream1. import java.io.*;2. class ReadFromKeyboardUsingBuffered { 3. public static void main(String[] args) 4. {5. byte[] input = new byte[1024];6. BufferedInputStream in = new

BufferedInputStream(System.in);7. System.out.print("Please enter your name here: ");8. try{9. in.read(input);10. System.out.println("Your name is " + new

String(input) );11. }12. catch (IOException e){13. System.out.println( e.getMessage() );14. }15. }16.}

Monday, November 5, 12

Page 7: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Character Stream classes• Reader

– In class BufferedReader• You can use readLind() to read 1 one from

keyboard. This method is useful.

• Writer

• Binary Stream– InputStream– OutputStream

• Character Stream– Reader– Writer

InputStream

InputStreamReader

Reader

Important: remember this

Monday, November 5, 12

Page 8: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Example of BufferedReader1. import java.io.*;2. class KeyboardReader {3. public static void main(String[] args) 4. {5. String name="";6. BufferedReader keyboard = new BufferedReader( 7. new InputStreamReader (System.in) );8. System.out.print("Please enter your name here: ");9. try{10. name = keyboard.readLine() ;11. }12. catch (IOException e){13. System.out.println( e.getMessage() );14. System.exit(1);15. }16. System.out.println("Your name is " + name);17. } InputStreamReader isr = new InputStreamReader (System.in);

BufferedReader keyboard = new BufferedReader(isr);Monday, November 5, 12

Page 9: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Example of reading integer from keyboard

1. import java.io.*;2. class ReadIntFromKeyboard {3. public static void main(String[] args) 4. { 5. int value=0;6. BufferedReader keyboard = new BufferedReader(7. new InputStreamReader (System.in) );8. System.out.print("Please enter your number here: ");9. try {10. String input = keyboard.readLine() ;11. value = Integer.parseInt(input);12. }13. catch (NumberFormatException e) {14. System.out.println("Error, please enter only integer

number");15. System.exit(1);16. }17. catch (IOException e) {18. System.out.println( "Error, cannot read from keyboard");19. System.exit(1);20. }Monday, November 5, 12

Page 10: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

StreamTokenizer

• Easy to perform input validation– double nval ถ้าข้อมูลที่อ่านมาเป็นตัวเลข ค่าของตัวเลขจะเก็บไว้ในตัวแปร

nval– String sval ถ้าข้อมูลที่อ่านเป็น String ค่าของ Sting จะอยู่ใน sval– int ttype ใช้ในการทดสอบว่าค่าที่อ่านเป็นข้อมูลชนิดใด เช่นถ้า ttype เท่ากับ

TT_NUMBER หมายถึงข้อมูลที่อ่านเป็นชนิดตัวเลข ถ้า ttype เท่ากับ TT_WORD แสดงว่าข้อมูลที่อ่านเป็นชนิด String

StreamTokenizer token = new StreamTokenizer( new BufferedReader(

new InputStreamReader(System.in) ) );

Monday, November 5, 12

Page 11: Introduction to OOP - Bangkok Universitytulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch8.pdf · Introduction to OOP Author: Gim Created Date: 11/5/2012 8:07:20 AM ...

Example of using StreamTokenizer1. import java.io.*;2. public class UsingStreamToken{3. public static void main(String[] args) {4. StreamTokenizer token = new StreamTokenizer (5. new BufferedReader (6. new InputStreamReader( System.in) ) );7. String input = "";8. while ( !input.equals("quit") ) // do until find "quit"9. {10. System.out.println("Please type \"quit\" to exit program");11. System.out.print("Please enter your input: ");12. int ttype=0;13. try {14. //Read data from keyboard15. ttype = token.nextToken();16. }17. catch (IOException e) {18. System.out.println("Error while reading from keyboard");19. System.exit(1);20. } // end try-catch21. 22. if(ttype == StreamTokenizer.TT_NUMBER) {23. System.out.println("You have enter a NUMBER " + 24. token.nval); 25. }

Programming Problems: 8.1

Monday, November 5, 12