session 4: java.io package

Post on 19-Jan-2016

47 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Session 4: java.io package. Review. Bài tập 1: construct an application that calculates the number of days remaining in the current year. Click to edit Master text styles Second level Third level Fourth level Fifth level. 1. Files and Directories. Constructors: - File(String path) - PowerPoint PPT Presentation

TRANSCRIPT

Session 4: java.io package

Review

Bài tập 1: construct an application that calculates the number of days remaining in the current year

Click to edit Master text stylesSecond level

Third level Fourth level

Fifth level

1. Files and Directories

Constructors:

- File(String path)

//1 para: is the path to a file or diretory

- File(String directoryPath, String filename)

//2 para: the path to a directory and the name of a file in that directory.

- File(File directory, String filename)

//2 para: a File object for a directory and the name of a file in that directory.

1. Files and Directories

boolean canRead //return true (file exists and can be read), false

boolean canWrite //return true (file exists and can be read), false

boolean delete //Deletes file. Return true (xóa thành công), false

boolean equals(Object obj) //return true (current object and obj refer to the same file)

boolean exists() //return true (the file exists)

1. Files and Directories

String getAbsolutePath() //return absolute path to the file

String getCanonicalPath() //return canonical path to the file

String getName() //return name of the file

String getParent() //return parent of the file

String getPath() //return path to the file

boolean isAbsolute() //return true (file path name is absolute)

boolean isDirectory() //return true (file is a directory)

1. Files and Directories

Boolean renameTo(File newName) // rename the file or directory to newName. Return true if (thay đổi thành công)

renameTo()

BT1: Write an application to rename a file. Use the renameTo() method of File to accomplish this task. The first command line argument is the old filename, the second is new filename

2. Character StreamsTo read and write characters and strings.

ReaderBufferedReader

InputStreamReader FileReader

InputStreamReader class extends Reader. It converts a stream of bytes to a stream of characters.

FileReader class extends InputStreamReader and inputs chracters from a file.

2. Character Streams

OutputStreamWritter class extends Writer. It converts a stream of characters to a stream of bytes.

FileWritter class extends OutputStreamWritter and outputs characters to a file.

Writer

BufferedWritter

OutputStreamWritter

PrintWritter

FileWritter

2. Character Streams

Ví dụ minh họa ghi và đọc file bằng cách sử dụng character streams

2. Character Streams

Click to edit Master text stylesSecond level

Third level Fourth level

Fifth level

• Ghi file

2. Character Streams

Đọc file

2. Character Streams

Bài tập về nhà 1:

Write an application that copies one character file to a second character file. The source file is the first command line argument and the destination file is the second command line argument.

3. Buffered Character Streams

BufferedWritter class extends Writer and buffers output to a character stream.

3. Buffered Character Streams

Ví dụ minh họa ghi và đọc file bằng cách sử dụng buffered character streams

3. Buffered Character Streams

Ghi file

3. Buffered Character Streams

Đọc file

4. Byte Stream

FileInputStream class extends InputStream and allows to read binary data from a file.

FilterInputStream class extends Inputstream and filters an input stream.

BufferedInputStream class extends FilterInputstream and buffers input from a byte stream.

DataInputStream class extends FilterInputStream and implements DataInput.

InputStreamFileInputStream

FilterInputStreamBufferedInputStream

DataInputStream

4. Byte Stream

FileOutputStream class extends OutputStream and allows to write binary data to a file.

FilterOutputStream class extends OutputStream , it is used to filter output.

BufferedOutputStream class extends FilterOutputStream and buffers output to a byte stream.

DataOutputStream class extends FilterOutputStream and inplements DataOutput.

OutputStreamFileOutputStream

FilterOutputStreamBufferedOutputStream

DataOutputStream

PrintStream

4. Byte Stream

Ví dụ minh họa 1: Sử dụng FileOutputStream và FileInputStream để ghi và đọc file

FileOutputStream và FileInputStream

Ghi file:

FileOutputStream và FileInputStream

Đọc file:

4. Byte Stream

Ví dụ minh họa 2: sử dụng BufferedOutputStream và BufferedInputStream để đọc và ghi file

BufferedOutputStream và BufferedInputStream

Ghi file

BufferedOutputStream và BufferedInputStream

Đọc file (BTVN 2)

4. Byte Stream

Ví dụ minh họa 3: sử dụng DataInputStream và DataOutputStream để ghi và đọc file

DataInputStream và DataOutputStream

Ghi file

DataInputStream và DataOutputStream

Đọc file (BTVN 3)

DataInputStream và DataOutputStream

BTVN 4: Write one application that writes the first 15 numbers of the Fibonacci series to a file. Use the writeShort() method of DataInputStream to output the numbers. Write a second application that reads this data from a file and display it. Use the readShort() method of DataOutputStream to input the number.

top related