Java I/O and Java Networking (Client Side) Yoshi.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Java I/O and Java Networking Yoshi. Recall that…
Streams and Files The objectives of this chapter are: To understand the principles of I/O streams and where to use them To understand the options and limitations.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Streams and Files CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Two Ways to Store Data in a File Text format Binary format.
Dynamic Arrays Dynamic arrays are arrays that are re- allocated to a larger size. –See Eck Section 8.3 for details. –For instance, what happens with the.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Richiami di Java Input/Output. import java.io.*; public class Simple { public static void main(String a[]){ new Simple(); } public Simple() { byte buffer[]=new.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
The Java I/O Classes and Interfaces cont’d
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Two Ways to Store Data in a File  Text format  Binary format.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
1 OOP Lecture 17 I/O and Graphics Signe Ellegård Borch Carsten Schuermann IT University Copenhagen.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java Programming Language (3) - Input/Output Stream –
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
CSC1351 Class 6 Classes & Inheritance.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Files and Streams The material in this chapter is not tested on the AP CS exams.
CSG2H3 Object Oriented Programming
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
OO Design and Programming II I/O: Reading and Writing
Lec 06 David Presentation on Advanced Spring
12: The Java I/O System stream model.
I/O Basics.
Java Methods ("Chapter %d", 14); Streams and Files A & AB
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Java’s Hierarchy Input/Output Classes and Their Purpose
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Stream Oriented I/O Computer Programming II Dr. Tim Margush
CSC 143 Java Streams.
Files and Streams in Java
Web Design & Development Lecture 8
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
David Davenport Spring 2005
Presentation transcript:

Java I/O and Java Networking (Client Side) Yoshi

Outline  Basic concepts  InputStream/OutputStream  Conversion and casting  IOException  Stream Redirection  Reader/Writer  Design in Reader/Writer  Socket

What is a Stream  Sequential, undetermined or undecided length of bits, such as  System.in  System.out  Stream redirection  java MessageServer < data.txt

Stream Concept Input Stream Output Stream

java.io.InputStream/OutputStream  BufferedInputStream  ByteArrayInputStream  DataInputStream  FileInputStream  FilterInputStream  ObjectInputStream  PipedInputStream  PushbackInputStream  SequenceInputStream  StringBufferedInputStream  LineNumberInputStream  BufferedOutputStream BufferedOutputStream  ByteArrayOutputStream  DataOutputStream  FileOutputStream  FilterOutputStream  ObjectOutputStream  PipedOutputStream  PrintStream PrintStream

 Deprecated in Java 1.1  Expected to be replaced by PrintWriter  Come back in Java 2  Note that System.out is an instance of PrintStream

Default Types  Integer number – int  byte b = 42; //OK  int i=42; byte = i; //wrong  int i=42; byte = (byte) i; //OK  byte = 1 + 2; //wrong  byte b1=1, b2=2; byte b3 = b1+b2; //wrong  Floating number – double  Similar as above

Conversion and Casting  For primitive types  Conversion  Narrow to wide  Casting  Wide to narrow, so information may be lost  int i=128;  byte b = (byte) i; //what is b?  Note that all the numeric types are signed

IOException  IOException is everywhere!  Note that it is a checked exception  We need to try/catch it  But in PrintStream/PrintWriter…  Exceptions are hided  public boolean checkError()

Something strange?  java.io.InputStream java.io.InputStream  public abstract int read() throws IOException  java.io.OutputStream java.io.OutputStream  public abstract void write(int b) throws IOException

Something strange? (2)  byte  -128 ~ +127  -1 implies end of stream  a-io-stream-read-write.html a-io-stream-read-write.html  You can also find it in O’Reilly Java I/O

Stream Redirection  Recall that  java MessageServer < data.txt  System.setOut System.setOut  System.setOut(new PrintStream(new FileOutputStream(“output.txt”)));  System.setIn System.setIn  System.setIn(new FileInputStream(“input.txt”));

File Operations  FileInputStream  FileOutputStream

Stream with Unicode  int b1 = in.read();  int b2 = in.read();  char c = (char) (b1*256 + b2);  Difficult to handle  That is why Java introduced Reader/Writer in Java 1.1

Character Stream  The Java platform stores character values using Unicode conventions  Character stream I/O automatically translates this internal format to and from the local character set.  Character streams are often wrappers for byte streams.  The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes.

Reader/Writer  BufferedReader  CharArrayReader  FileReader  FilterReader  InputStreamReader  PipedReader  StringReader  PushbackReader  LinedNumberReader  BufferedWriter  CharArrayWriter  FileWriter  FilterWriter  OutputStreamWriter  PipedWriter  StringWriter  PrintWriter

File Operations  InputStreamReader  A bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset  FileInputStream fin = new FileInputStream(“input.txt”);  InputStreamReader isr = new InputStreamReader(fin, “Big5”);  OutputStreamWriter  Similar as above  FileOutputStream fos = new FileOutputStream(“output.txt”);  OutputStreamWriter osw = new OutputStreamWriter(fos,”Big5”);

FileReader/FileWriter  FileWriter fw = new FIleWriter(“output.txt”);  FileReader fr = new FIleReader(“input.txt”);  Default charset  Just for convenience

Line-Oriented Reader/Writer public class CopyLines { public static void main(String[] args) throws IOException { BufferedReader inputReader = null; PrintWriter outputWriter = null; inputReader = new BufferedReader(new FileReader(“input.txt")); outputWriter = new PrintWriter(new FileWriter("output.txt")); String line; while ((line = inputReader.readLine()) != null) { outputWriter.println(line); } inputReader.close(); outputWriter.close(); }

Recall that…

Recall that…and Questions?  Do Reader/Writer have the same design?  java.io.FilterReader java.io.FilterReader  java.io.FilterWriter java.io.FilterWriter Reader InputStreamReaderBufferedReaderFilterReader

No Explanation  In Thinking in Java, Chapter 11  It mentioned that BufferedReader is not a subclass of FilterReader, but no explanation  In O’Reilly Java I/O, no explanation  Anyone can answer me?

Let’s trace BufferedInputStream  fferedInputStream.java.html fferedInputStream.java.html

java.net.Socket  This class implements client sockets (also called just "sockets").  A socket is an endpoint for communication between two machines.  Very similar to local I/O operations

Retrieve Yahoo! Homepage import java.io.*; import java.net.*; public class RetrieveWeb { public RetrieveWeb(String host, int port) throws IOException { Socket socket = new Socket(host, port); InputStream in = socket.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(in, "UTF8")); OutputStream out = socket.getOutputStream(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(out)); pw.println("GET /"); pw.flush(); String line=""; while( (line=reader.readLine()) != null) { System.out.println(line);

Retrieve Yahoo! Homepage } reader.close(); pw.close(); } public static void main(String[] args) throws IOException { //delete the following three lines to make this program general args = new String[2]; args[0] = "tw.yahoo.com"; args[1] = "80"; int port = Integer.parseInt(args[1]); RetrieveWeb obj = new RetrieveWeb(args[0], port); }

Exercise  Trace java.io.BufferedReader  edReader.java.html edReader.java.html  Write a program to connect to hulabear.twbbs.org:23