Bài giảng Lập trình mạng: Stream - Nguyễn Hữu Thể
lượt xem 3
download
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.
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: 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. 2
- InputStream 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. 4
- OutputStream 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. 7
- EX: Input character import 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. 10
- FileInputStream SN Methods with Description public void close() throws IOException{} This method closes the file 1 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 output 2 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 specified 3 byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if it's end of file. public int read(byte[] r) throws IOException{} This method reads r.length 4 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 that 5 can be read from this file input stream. Returns an int. 11
- FileInputStream public 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 doesn't already exist, before opening it for output. 13
- FileOutputStream SN Methods with Description public void close() throws IOException{} This method 1 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 close 2 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 methods 3 writes the specified byte to the output stream. public void write(byte[] w) Writes w.length bytes from the 4 mentioned byte array to the OutputStream. 14
- FileOutputStream import java.io.*; public class FileOut { public static void main(String args[]) { try { FileOutputStream fout = new FileOutputStream("abc.txt"); String s = "Hello World"; byte b[] = s.getBytes();//string to byte array fout.write(b); fout.close(); System.out.println("Success..."); } catch (Exception e) { System.out.println(e); } } 15
- Byte Streams ― Java byte streams are used to perform input and output of 8-bit bytes. public class CopyFile { public static void main(String args[]) throws IOException{ FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) in.close(); if (out != null) out.close(); } } 16 }
- Character Streams ― Character streams are used to perform input and output for 16-bit unicode. ― Classes: FileReader and FileWriter. 17
- FileReader class ― FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class Constructor Description It gets filename in string. It opens the FileReader(String file) given file in read mode. If file doesn't exist, it throws FileNotFoundException. It gets filename in file instance. It opens FileReader(File file) the given file in read mode. If file doesn't exist, it throws FileNotFoundException. Method Description returns a character in ASCII form. It 1) public int read() returns -1 at the end of file. 2) public void close() closes FileReader. 18
- FileReader class - EX import java.io.*; public class ReadFile { public static void main(String args[]) throws Exception { FileReader fr = new FileReader("abc.txt"); int i; while ((i = fr.read()) != -1) System.out.println((char) i); fr.close(); } } 19
- FileWriter class ― Write character-oriented data to the file. Constructor Description creates a new file. It gets file FileWriter(String file) name in string. creates a new file. It gets file FileWriter(File file) name in File object. Method Description 1) public void write(String text) writes the string into FileWriter. 2) public void write(char c) writes the char into FileWriter. 3) public void write(char[] c) writes char array into FileWriter. 4) public void flush() flushes the data of FileWriter. 5) public void close() closes FileWriter. 20
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Giáo trình lập trình truyền thông part 5
10 p | 209 | 55
-
Bài giảng Lập trình Java cơ bản: Chương 5 Nhập xuất - GV. Võ Hoàng Phương Dung
19 p | 115 | 16
-
Bài giảng Lập trình mạng: Chương 2 - ThS. Trần Bá Nhiệm
28 p | 88 | 9
-
Bài giảng Lập trình mạng: Chương 2 - ThS. Trần Đắc Tốt
49 p | 23 | 7
-
Bài giảng Lập trình mạng: Chương 2 - ThS. Trần Đắc Tốt
49 p | 71 | 6
-
Bài giảng Lập trình mạng: Chương 3 - ĐH Công nghệ Đồng Nai
49 p | 50 | 4
-
Bài giảng Mạng máy tính - Chương 7.1: Lập trình socket
18 p | 47 | 3
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn