Download - Session 4: java.io package
![Page 1: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/1.jpg)
Session 4: java.io package
![Page 2: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/2.jpg)
Review
Bài tập 1: construct an application that calculates the number of days remaining in the current year
![Page 3: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/3.jpg)
Click to edit Master text stylesSecond level
Third level Fourth level
Fifth level
![Page 4: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/4.jpg)
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.
![Page 5: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/5.jpg)
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)
![Page 6: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/6.jpg)
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)
![Page 7: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/7.jpg)
1. Files and Directories
Boolean renameTo(File newName) // rename the file or directory to newName. Return true if (thay đổi thành công)
![Page 8: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/8.jpg)
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
![Page 9: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/9.jpg)
![Page 10: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/10.jpg)
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.
![Page 11: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/11.jpg)
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
![Page 12: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/12.jpg)
2. Character Streams
Ví dụ minh họa ghi và đọc file bằng cách sử dụng character streams
![Page 13: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/13.jpg)
2. Character Streams
Click to edit Master text stylesSecond level
Third level Fourth level
Fifth level
• Ghi file
![Page 14: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/14.jpg)
2. Character Streams
Đọc file
![Page 15: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/15.jpg)
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.
![Page 16: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/16.jpg)
3. Buffered Character Streams
BufferedWritter class extends Writer and buffers output to a character stream.
![Page 17: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/17.jpg)
3. Buffered Character Streams
Ví dụ minh họa ghi và đọc file bằng cách sử dụng buffered character streams
![Page 18: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/18.jpg)
3. Buffered Character Streams
Ghi file
![Page 19: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/19.jpg)
3. Buffered Character Streams
Đọc file
![Page 20: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/20.jpg)
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
![Page 21: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/21.jpg)
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
![Page 22: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/22.jpg)
4. Byte Stream
Ví dụ minh họa 1: Sử dụng FileOutputStream và FileInputStream để ghi và đọc file
![Page 23: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/23.jpg)
FileOutputStream và FileInputStream
Ghi file:
![Page 24: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/24.jpg)
FileOutputStream và FileInputStream
Đọc file:
![Page 25: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/25.jpg)
4. Byte Stream
Ví dụ minh họa 2: sử dụng BufferedOutputStream và BufferedInputStream để đọc và ghi file
![Page 26: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/26.jpg)
BufferedOutputStream và BufferedInputStream
Ghi file
![Page 27: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/27.jpg)
BufferedOutputStream và BufferedInputStream
Đọc file (BTVN 2)
![Page 28: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/28.jpg)
4. Byte Stream
Ví dụ minh họa 3: sử dụng DataInputStream và DataOutputStream để ghi và đọc file
![Page 29: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/29.jpg)
DataInputStream và DataOutputStream
Ghi file
![Page 30: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/30.jpg)
DataInputStream và DataOutputStream
Đọc file (BTVN 3)
![Page 31: Session 4: java.io package](https://reader036.vdocuments.mx/reader036/viewer/2022062322/56814e56550346895dbbed37/html5/thumbnails/31.jpg)
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.