Bài giảng Lập trình mạng: Stream - Nguyễn Hữu Thể
Số trang: 22
Loại file: pdf
Dung lượng: 838.11 KB
Lượt xem: 1
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng "Lập trình mạng: Stream" cung cấp cho người đọc các kiến thức: InputStream, InputStream class, OutputStream class, OutputStream, Standard Streams, Reading and Writing Files,... Mời các bạn cùng tham khảo nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình mạng: Stream - Nguyễn Hữu ThểLẬP TRÌNH MẠNG STREAM Nguyễn Hữu Thể STREAM― A stream can be defined as a sequence of data. There are two kinds of Streams InputStream: The InputStream is used to read data from a source. OutputStream: the OutputStream is used for writing data to a destination. 2InputStream 3 InputStream class― InputStream class is an abstract class.It is the superclass of all classes representing an input stream of bytes. Method Description reads the next byte of data from 1) public abstract int the input stream.It returns -1 at read()throws IOException: the end of file. returns an estimate of the number 2) public int available()throws of bytes that can be read from the IOException: current input stream. 3) public void close()throws is used to close the current input IOException: stream. 4OutputStream 5 OutputStream class― OutputStream class is an abstract class.― It is the superclass of all classes representing an output stream of bytes. Method Description 1) public void write(int) throws is used to write a byte to the IOException: current output stream. 2) public void write(byte[]) is used to write an array of byte throws IOException: to the current output stream. 3) public void flush() throws flushes the current output IOException: stream. 4) public void close() throws is used to close the current IOException: output stream. 6 Standard Streams― Support for standard I/O Standard Input: usually a keyboard is used as standard input stream and represented as System.in. Standard Output: usually a computer screen is used to standard output stream and represented as System.out. Standard Error: usually a computer screen is used to standard error stream and represented as System.err. 7EX: Input characterimport java.io.*;public class ReadConsole { public static void main(String args[]) throws IOException { InputStreamReader cin = null; try { cin = new InputStreamReader(System.in); System.out.println(Enter characters, q to quit.); char c; do { c = (char) cin.read(); System.out.print(c); } while (c != q); } finally { if (cin != null) { cin.close(); } } }} 8 Reading and Writing Files― A stream can be defined as a sequence of data. InputStream is used to read data from a source OutputStream is used for writing data to a destination.― Hierarchy of classes: 9 FileInputStream― Reading data from the files. Objects can be created using the keyword new and there are several types of constructors available. 10FileInputStreamSN Methods with Description public void close() throws IOException{} This method closes the file1 output stream. Releases any system resources associated with the file. Throws an IOException. protected void finalize()throws IOException {} This method cleans up the connection to the file. Ensures that the close method of this file output2 stream is called when there are no more references to this stream. Throws an IOException. public int read(int r)throws IOException{} This method reads the specified3 byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if its end of file. public int read(byte[] r) throws IOException{} This method reads r.length4 bytes from the input stream into an array. Returns the total number of bytes read. If end of file -1 will be returned. public int available() throws IOException{} Gives the number of bytes that5 can be read from this file input stream. Returns an int. 11 FileInputStreampublic class FileInput { public static void main(String args[]) { try { FileInputStream fin = new FileInputStream(abc.txt); int i = 0; while ((i = fin.read()) != -1) { System.out.println((char) i); } fin.close(); } catch (Exception e) { System.out.println(e); } }} 12 FileOutputStream― Create a file and write data into it. The stream would create a file, if it doesnt already exist, before opening it for output. 13FileOutputStreamSN Methods with Description public void close() throws IOException{} This method1 closes the file output stream. Releases any system resources associated with the file. Throws an IOException protected void finalize()throws IOException {} This method cleans up the connection to the file. Ensures that the close2 method of this file output stream is called when there are no more references to this stream. Throws an IOException. public void write(int w)throws IOException{} This me ...
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình mạng: Stream - Nguyễn Hữu ThểLẬP TRÌNH MẠNG STREAM Nguyễn Hữu Thể STREAM― A stream can be defined as a sequence of data. There are two kinds of Streams InputStream: The InputStream is used to read data from a source. OutputStream: the OutputStream is used for writing data to a destination. 2InputStream 3 InputStream class― InputStream class is an abstract class.It is the superclass of all classes representing an input stream of bytes. Method Description reads the next byte of data from 1) public abstract int the input stream.It returns -1 at read()throws IOException: the end of file. returns an estimate of the number 2) public int available()throws of bytes that can be read from the IOException: current input stream. 3) public void close()throws is used to close the current input IOException: stream. 4OutputStream 5 OutputStream class― OutputStream class is an abstract class.― It is the superclass of all classes representing an output stream of bytes. Method Description 1) public void write(int) throws is used to write a byte to the IOException: current output stream. 2) public void write(byte[]) is used to write an array of byte throws IOException: to the current output stream. 3) public void flush() throws flushes the current output IOException: stream. 4) public void close() throws is used to close the current IOException: output stream. 6 Standard Streams― Support for standard I/O Standard Input: usually a keyboard is used as standard input stream and represented as System.in. Standard Output: usually a computer screen is used to standard output stream and represented as System.out. Standard Error: usually a computer screen is used to standard error stream and represented as System.err. 7EX: Input characterimport java.io.*;public class ReadConsole { public static void main(String args[]) throws IOException { InputStreamReader cin = null; try { cin = new InputStreamReader(System.in); System.out.println(Enter characters, q to quit.); char c; do { c = (char) cin.read(); System.out.print(c); } while (c != q); } finally { if (cin != null) { cin.close(); } } }} 8 Reading and Writing Files― A stream can be defined as a sequence of data. InputStream is used to read data from a source OutputStream is used for writing data to a destination.― Hierarchy of classes: 9 FileInputStream― Reading data from the files. Objects can be created using the keyword new and there are several types of constructors available. 10FileInputStreamSN Methods with Description public void close() throws IOException{} This method closes the file1 output stream. Releases any system resources associated with the file. Throws an IOException. protected void finalize()throws IOException {} This method cleans up the connection to the file. Ensures that the close method of this file output2 stream is called when there are no more references to this stream. Throws an IOException. public int read(int r)throws IOException{} This method reads the specified3 byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if its end of file. public int read(byte[] r) throws IOException{} This method reads r.length4 bytes from the input stream into an array. Returns the total number of bytes read. If end of file -1 will be returned. public int available() throws IOException{} Gives the number of bytes that5 can be read from this file input stream. Returns an int. 11 FileInputStreampublic class FileInput { public static void main(String args[]) { try { FileInputStream fin = new FileInputStream(abc.txt); int i = 0; while ((i = fin.read()) != -1) { System.out.println((char) i); } fin.close(); } catch (Exception e) { System.out.println(e); } }} 12 FileOutputStream― Create a file and write data into it. The stream would create a file, if it doesnt already exist, before opening it for output. 13FileOutputStreamSN Methods with Description public void close() throws IOException{} This method1 closes the file output stream. Releases any system resources associated with the file. Throws an IOException protected void finalize()throws IOException {} This method cleans up the connection to the file. Ensures that the close2 method of this file output stream is called when there are no more references to this stream. Throws an IOException. public void write(int w)throws IOException{} This me ...
Tìm kiếm theo từ khóa liên quan:
Bài giảng Lập trình mạng Lập trình mạng Mạng máy tính Reading and Writing Files Kỹ thuật lập trình Ngôn ngữ lập trìnhGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 270 0 0 -
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 262 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 260 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 259 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 250 1 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 242 0 0 -
47 trang 236 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 234 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 230 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 220 0 0