Lập trình mạng 7 chuyên đề java io stream

88 965 2
Lập trình mạng 7 chuyên đề java io stream

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Dùng để viết dữ liệu dạng byte từ OutputStream vào file.Its constructors are :FileOutputStream(String filename) throws FileNotFoundException : Creates an OutputStream that we can use to write bytes to a file.FileOutputStream(File name) throws FileNotFoundException : Creates an OutputStream that we can use to write bytes to a file.FileOutputStream(String filename, boolean flag) throws FileNotFoundException : Creates an OutputStream that we can use to write bytes to a file. If flag is true, file is opened in append mode.

Chapter 7 Input/Output Streams 1 Java Simplified / Session 22 / 2 of 45  Explain the concept of streams  Explain the standard input/output streams  Explain the ByteStream I/OStream ◦ Explain the classes: FileInputStream and FileOutputStream ◦ Describe the Byte array I/O ◦ Discuss Filtered and Buffered I/O operations ◦ Discuss the class RandomAccessFile, File  Explain the CharacterStream ◦ Explain classes: Write, Reader, OutputStreamWriter, InputStreamReader, FileWriter, FileReader.  Explain BufferedStream  Explain Serialization Objectives 2 Java Simplified / Session 22 / 3 of 45  Stream là các dãy dữ liệu có sắp thứ tự  Truyền dữ liệu cho một chương trình Java: ◦ Thông qua giao diện người máy. ◦ Thông qua một tài nguyên tuần tự nào đó như file hoặc qua một máy tính khác.  Java nhận và gửi dữ liệu thông qua các đối tượng là các thực thể thuộc một kiểu luồng dữ liệu nào đó.  Phân loại luồng thành hai loại: ◦ Luồng xuất (output stream) và ◦ luồng nhập (input stream). Streams 3 Java Simplified / Session 22 / 4 of 45 I/O Stream 4 Java Simplified / Session 22 / 5 of 45  An I/O Stream represents an input source or an output destination.  A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.  Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects.  Some streams simply pass on data; others manipulate and transform the data in useful ways I/O Stream 5 Java Simplified / Session 22 / 6 of 45 Stream Console Devices File Network 6 Java Simplified / Session 22 / 7 of 45  Khi 1 stream được đọc hoặc ghi thì các các luồng khác sẽ bị khóa.  Ngoại lệ: trong khi đọc hoặc ghi 1 stream, nếu xảy ra lỗi: IOException is thrown. ◦ Dùng kết hợp với khối try/ catch block. Streams Contd… 7 Java Simplified / Session 22 / 8 of 45  Standard input/output stream in Java is represented by three fields of the System class : ◦ System.in, ◦ System.out and ◦ System.err. Standard input/output stream 8 Java Simplified / Session 22 / 9 of 45 Example class BasicIO { public static void main(String args[]) { byte bytearr[] = new byte[30]; try { System.out.println("Enter a line of text"); System.in.read(bytearr,0,30); System.out.println("The line typed was "); String str = new String(bytearr); System.out.println(str); } catch(Exception e) { System.out.println("Error occurred!"); } } } 9 Java Simplified / Session 22 / 10 of 45 There are two main categories of streams in Java :  Byte Streams – Cung cấp cách thao tác việc nhập/ xuất theo byte InputStream and OutputStream classes are at the top of their hierarchy.  Character Streams – Cung cấp cách thao tác việc nhập/ xuất theo character. The java.io package 10 [...]... RandomAccessFile DataInput DataOutput InputStream FileInput Stream ByteArray InputStream DataInput Stream OutputStream Filter InputStream Buffered InputStream FileOutput Stream LineNumber InputStream Filter OutputStream PushBack InputStream Buffered OutputStream ByteArray OutputStream DataOutput Stream Print Stream Java Simplified / Session 22 / 11 of 45 Byte Streams  Hai lớp trừu tượng làm cơ sở để... FileOutputStream ◦ ByteArrayInputStream, ByteArrayOutputStream ◦ FilterInputStream, FilterOutputStream  BufferedInputStream, BufferedOutputStream Java Simplified / Session 22 / 15 of 45 Methods in InputStream class  Đọc từng byte từ tệp ◦ int read() throws IOException Return value (8 low bits of int): 0-255; end of stream: -1 ◦ int read(byte[] b) throws IOException Read data from input stream to array b ◦ int... try { FileOutputStream fos = new FileOutputStream("aa.txt"); fos.write(b,0,b.length); Java Simplified / Session 22 / 25 of 45 Ex:Copy a file with read(byte[] data) and write(byte[] data) import java. io. File; import java. io. FileInputStream; import java. io. FileOutputStream; public class Main { public static void main(String[] args) throws Exception { FileInputStream fin = null; FileOutputStream fout =... args[]) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); String s = "This is a test."; for (int i = 0; i < s.length(); ++i) outStream.write(s.charAt(i)); System.out.println("outstream: " + outStream); System.out.println("size: " + outStream.size()); ByteArrayInputStream inStream; inStream = new ByteArrayInputStream(outStream.toByteArray()); int inBytes = inStream.available();... len) throws IOException Read data from input stream to array b, start offs, save len bytes  public int available() throws IOException ◦ Numbers data bytes left in Stream Java Simplified / Session 22 / 16 of 45 Methods in InputStream class  public long skip(long count) throws IOException ◦ Skip count data bytes  public synchronized void mark(int readLimit) ◦ Mark current position in InputStream  public... Session 22 / 13 of 45    Xác định cách ghi dữ liệu vào luồng output Định nghĩa các method để ghi dữ liệu Các các class con OutputStream OutputStream class FileOutputStream ByteArrayOutputStream FilterOutputStream ObjectOutputStream Java Simplified / Session 22 / 14 of 45 I/O Stream Class Các phương thức của I/O Stream class  Các lớp con: (constructors, methods)  ◦ FileInputStream, FileOutputStream... InputStream class  File/ByteArray/Filter/ObjectInputStream ◦ OutputStream class  File/ByteArray/Filter/ObjectOutputStream Java Simplified / Session 22 / 12 of 45 InputStream class Xác định cách thức lấy dữ liệu  Cung cấp cách đọc dữ liệu từ 1 luồng nhập: Định nghĩa các method để đọc dữ liệu  Có các lớp con InputStream  FileInputStream ByteArrayInputStream FilterInputStream ObjectInputStream Java. .. String to InputStream Example"; InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8")); Java Simplified / Session 22 / 28 of 45 Ex: Convert string into InputStream using ByteArrayInputStream class import java. io. *; class ByteDemo { public static void main (String []args) { String str = "Độc lập tự do hạnh phúc Việt Nam"; byte[] b = str.getBytes(); InputStream bais = new ByteArrayInputStream(b,0,0);... InputStream  public void reset() throws IOException ◦ Redefine recent marked position in InputStream  public boolean markSupported() ◦ True: support mark, flase: not support  public void close() throws IOException ◦ Close file Release resource Java Simplified / Session 22 / 17 of 45 Methods in OutputStream class  Ghi từng byte vào tệp: ◦ void write(int b) throws IOException Write every byte to file (8... FileNotFoundException : Creates an InputStream that we can use to read bytes from a filename (string) ◦ FileInputStream(File filename) throws FileNotFoundException : Creates an input stream that we can use to read bytes from a file where filename is a File object Java Simplified / Session 22 / 19 of 45 Ex: Read and display data from file import java. io. FileInputStream; class BasicIO { public static . interfaces 11 Object File FileDescriptor RandomAccessFile DataInput DataOutput DataInput Stream Buffered InputStream LineNumber InputStream PushBack InputStream Filter InputStream InputStream ByteArray InputStream FileInput Stream OutputStream FileOutput Stream Filter OutputStream ByteArray OutputStream Buffered OutputStream DataOutput Stream Print Stream Java. class  File/ByteArray/Filter/ObjectInputStream ◦ OutputStream class  File/ByteArray/Filter/ObjectOutputStream Byte Streams 12 Java Simplified / Session 22 / 13 of 45 InputStream FileInputStream ByteArrayInputStream FilterInputStream ObjectInputStream  Xác. ◦ Luồng xuất (output stream) và ◦ luồng nhập (input stream) . Streams 3 Java Simplified / Session 22 / 4 of 45 I/O Stream 4 Java Simplified / Session 22 / 5 of 45  An I/O Stream represents an

Ngày đăng: 09/07/2014, 15:07

Từ khóa liên quan

Mục lục

  • Slide 1

  • Objectives

  • Streams

  • I/O Stream

  • I/O Stream

  • Stream

  • Streams Contd…

  • Standard input/output stream

  • Example

  • The java.io package

  • Hierarchy of classes and interfaces

  • Byte Streams

  • InputStream class

  • OutputStream class

  • I/O Stream Class

  • Methods in InputStream class

  • Methods in InputStream class

  • Methods in OutputStream class

  • FileInputStream class

  • Ex: Read and display data from file

Tài liệu cùng người dùng

Tài liệu liên quan