1 / 31

Session 4: java.io package

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)

nerita
Download Presentation

Session 4: java.io package

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 4: java.io package

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

  3. Click to edit Master text styles • Second level • Third level • Fourth level • Fifth level

  4. 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.

  5. 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)

  6. 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)

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

  8. 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

  9. 2. Character StreamsTo read and write characters and strings. BufferedReader Reader 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.

  10. 2. Character Streams BufferedWritter Writer OutputStreamWritter FileWritter PrintWritter 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.

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

  12. 2. Character Streams • Ghi file • Click to edit Master text styles • Second level • Third level • Fourth level • Fifth level

  13. 2. Character Streams Đọc file

  14. 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.

  15. 3. Buffered Character Streams BufferedWritter class extends Writer and buffers output to a character stream.

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

  17. 3. Buffered Character Streams Ghi file

  18. 3. Buffered Character Streams Đọc file

  19. 4. Byte Stream FileInputStream InputStream BufferedInputStream FilterInputStream DataInputStream 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.

  20. 4. Byte Stream FileOutputStream OutputStream BufferedOutputStream FilterOutputStream DataOutputStream PrintStream 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.

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

  22. FileOutputStream và FileInputStream Ghi file:

  23. FileOutputStream và FileInputStream Đọc file:

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

  25. BufferedOutputStream và BufferedInputStream Ghi file

  26. BufferedOutputStream và BufferedInputStream Đọc file (BTVN 2)

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

  28. DataInputStream và DataOutputStream Ghi file

  29. DataInputStream và DataOutputStream Đọc file (BTVN 3)

  30. 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.

More Related