Một số hàm chức năng:
 public abstract void write(int b) throws IOException 
 public void write(byte[] b) throws IOException 
 public void write(byte[] b, int off, int len ) throws IOException 
 public void flush() throws IOException
 public void close() throws IOException
                
              
                                            
                                
            
                       
            
                 28 trang
28 trang | 
Chia sẻ: haohao89 | Lượt xem: 2452 | Lượt tải: 0 
              
            Bạn đang xem trước 20 trang tài liệu Bài giảng Các luồng vào/ra và các tệp dữ liệu, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Các luồng vào/ra Lớp File Các luồng vào/ra xử lý theo byte Đọc, ghi các ký tự: Reader, Writer Truy nhập tệp ngẫu nhiên Truy nhập tệp tuần tự dựa trên đối tượng. CHƯƠNG VII Các luồng vào/ra và các tệp dữ liệu 1. Các luồng vào/ra Java sử dụng các luồng Stream như là cơ chế chung để xử lý I/O. Có hai loại Stream: Stream được sử dụng để xử lý các byte Stream để xử lý các ký tự Lớp InputStream được sử dụng để đọc, nhập dữ liệu vào chương trình và lớp OutputStream được sử dụng để ghi dữ liệu tuần tự lên tệp. 2. Lớp File Thuộc tính: public static final char separatorChar public static final String separator 	public static final char pathSeparatorChar 	public static final String pathSeparator Toán tử tạo lập: public File(String pathName) public File(File parent, String child) public File(String parent, String child) public File(URI uri) Một số hàm cơ bản của lớp File: public String getName() public String getPath() public String getAbsolutePath() public String getParent() 	public File getParentFile() public long length() public boolean exists() public boolean isFile() public boolean isDirectory() public boolean createNewFile() throws IOException public boolean mkdir() public boolean mkdirs() public boolean renameTo(File dest) public boolean delete() public String[] list() public File[] listFiles() ….. 3. Các luồng vào/ra xử lý theo byte Các lớp đọc dữ liệu vào theo các byte Các lớp ghi dữ liệu ra tệp theo các byte a. Lớp trừu tượng InputStream Một số hàm chức năng: 	public abstract int read() throws IOException 	public int read(byte[] b) throws IOException 	public int read(byte[] b, int off, int len ) throws IOException 	public long skip(long n) throws IOException 	public void close() throws IOException 	… b. Lớp trừu tượng OutputStream Một số hàm chức năng: 	public abstract void write(int b) throws IOException 	public void write(byte[] b) throws IOException 	public void write(byte[] b, int off, int len ) throws IOException 	public void flush() throws IOException 	public void close() throws IOException 	… c. Lớp FileInputStream Cấu tử khởi tạo: 	FileInputStream(String name) throws FileNotFoundedException 	FileInputStream(File file) throws FileNotFoundedException 	FileInputStream(FileDescriptor fdObj) Một số hàm chức năng: 	public int read() throws IOException 	public int read(byte[] b) throws IOException 	public int read(byte[] b, int off, int len ) throws IOException 	public long skip(long n) throws IOException 	public void close() throws IOException d. Lớp FileOutputStream Cấu tử khởi tạo: 	FileOutputStream(String name) throws FileNotFoundedException FileOutInputStream(File file) throws FileNotFoundedException FileOutInputStream(FileDescriptor fdObj) 	FileOutInputStream(String name, boolean append ) 	throws FileNotFoundedException FileOutInputStream(File file , boolean append ) 	throws FileNotFoundedException Một số hàm chức năng: 	public void write(int b) throws IOException 	public void write(byte[] b) throws IOException 	public void write(byte[] b, int off, int len ) throws IOException 	public void close() throws IOException e. Lớp FilterInputStream Cấu tử khởi tạo: 	FilterInputStream(InputStream in) Một số hàm chức năng: 	public int read() throws IOException 	public int read(byte[] b) throws IOException 	public int read(byte[] b, int off, int len ) throws IOException 	public long skip(long n) throws IOException 	public void close() throws IOException f. Lớp FilterOutputStream Cấu tử khởi tạo: 	FileOutputStream(OutputStream out) Một số hàm chức năng: 	public void write(int b) throws IOException 	public void write(byte[] b) throws IOException 	public void write(byte[] b, int off, int len ) throws IOException 	public void close() throws IOException 	public void flush() throws IOException g. Giao diện DataInput Một số hàm chức năng: 	public String readLine() 	public boolean readBoolean() 	public double readDouble() 	public float readFloat() 	public int readInt() 	public char readChar() 	… h. Giao diện DataOutput Một số hàm chức năng: 	public void write(int b) throws IOException 	public void write(byte[] b) throws IOException 	public void write(byte[] b, int off, int len ) throws IOException 	public void writeBoolean(boolean b) throws IOException 	public void writeDouble(double d) throws IOException 	public void writeChar(int i) throws IOException 	… i. Lớp DataInputStream Cấu tử khởi tạo: 	DataInputStream(InputStream in) Một số hàm chức năng: 	public final int read(byte[] b) throws IOException 	public final int read(byte[] b, int off, int len ) throws IOException 	public final boolean readBoolean() throws IOException 	public final byte readByte() throws IOException 	public final short readShort() throws IOException 	…. j. Lớp DataOutputStream Cấu tử khởi tạo: 	DataOutputStream(OutputStream in) Một số hàm chức năng: 	public void flush() throws IOException 	public final int size() throws IOException 	public void write(int b) throws IOException 	public void write(byte[] b, int off, int len ) throws IOException 	public final void writeBoolean(boolean b) throws IOException 	public final void writeFloat(float f) throws IOException 	…. 4. Đọc, ghi các ký tự Reader/Writer Các lớp đọc dữ liệu vào Các lớp ghi dữ liệu a. Lớp trừu tượng Reader Một số hàm chức năng: 	public int read() throws IOException 	public int read(char[] c) throws IOException 	public abstract int read(char[] c, int off, int len ) throws IOException 	public long skip(long n) throws IOException 	public abstract void close() throws IOException b. Lớp trừu tượng Writer Một số hàm chức năng: 	public void write(int c) throws IOException 	public void write(char[] c) throws IOException 	public abstract void write(char[] c, int off, int len ) throws IOException 	public void write(String s) throws IOException 	public void write(String s, int off, int len ) throws IOException 	public abstract void flush() throws IOException 	public abstract void close() throws IOException a. Lớp BufferedReader Cấu tử khởi tạo: 	BufferedReader(Reader in) 	 BufferedReader(Reader in, int sz) Một số hàm chức năng: 	public int read() throws IOException 	public int read(char[] c, int off, int len ) throws IOException 	public String readLine() throws IOException 	public long skip(long n) throws IOException 	public void close() throws IOException b. Lớp BufferedWriter Cấu tử khởi tạo: 	BufferedWriter(Writer out) BufferedWriter(Writer out, int sz) Một số hàm chức năng: 	public void write(int i) throws IOException 	public void write(char[] c, int off, int len ) throws IOException 	public void write(String s, int off, int len ) throws IOException 	public void flush() throws IOException 	public void close() throws IOException c. Lớp InputStreamReader Cấu tử khởi tạo: 	InputStreamReader(InputStream in) 	InputStreamReader(InputStream in, String charsetName) 	InputStreamReader(InputStream in, Charset cs) 	InputStreamReader(InputStream in, CharsetDecoder dec) Một số hàm chức năng: 	public int read() throws IOException 	public int read(char[] cbuff, int off, int len ) throws IOException 	public void close() throws IOException d. Lớp OutputStreamWriter Cấu tử khởi tạo: 	OutputStreamWriter(OutputStream out) 	OutputStreamWriter(OutputStream out, String charsetName) OutputStreamWriter(OutputStream out, Charset cs) 	OutputStreamWriter(OutputStream out, CharsetDecoder dec) Một số hàm chức năng: 	public void write(int c) throws IOException 	public void write(char[] cbuff, int off, int len ) throws IOException 	public void write(String s, int off, int len ) throws IOException 	public void flush() throws IOException 	public void close() throws IOException 5. Truy nhập tệp ngẫu nhiên Lớp truy nhập tệp ngẫu nhiên Cấu tử khởi tạo: 	RandomAccessFile(String name, String mode ) 	throws FileNotFoundException 	RandomAccessFile(File file, String mode ) 	throws FileNotFoundException Một số hàm chức năng: 	public long getFilePointter() throws IOException 	public long length() throws IOException 	public void seek(long offset) throws IOException 	public void close() throws IOException 	… Trong đó mode: nhận một trong các giá trị: “r”, “rw”, “rws”, “rwd” 6. Truy nhập tệp tuần tự dựa trên đối tượng a. Lớp ObjectInputStream Cấu tử khởi tạo: 	ObjectInputStream() throws IOException, SecurityException 	ObjectInputStream(InputStream in) throws IOException Một số hàm chức năng: 	public final Object readObject() throws IOException, 	ClassNotFoundException 	public void close() throws IOException 	… b. Lớp ObjectOutputStream Cấu tử khởi tạo: 	ObjectOutputStream() throws IOException, SecurityException 	ObjectOutputStream(OutputStream out) throws IOException Một số hàm chức năng: 	public final void writeObject(Object obj) throws IOException 	public void flush() throws IOException 	public void close() throws IOException 	…