Java-based I/O Stream (1)

Source: Internet
Author: User

To sum up the knowledge and concepts related to the basic classes for reading and writing Java I/O files, it is a difficult task for programmers to create a good input/output system. The challenge comes from all possibilities, there are not only various source and acceptor files, consoles, network connections, etc.), but also different ways to communicate with them in order, random access, buffering, binary, by character, by line, by word, etc ).

Java I/O mainly includes the following layers:

1. File

It is independent from the system platform and uses its constructor to create the corresponding File object. Then, it calls the method to perform operations on all attributes of the File.

Constructor:

  • File (String path)
  • File (String path, String FileName)
  • File (File dir, String name)

Purpose: The File class provides a machine-independent method to describe the attributes of a File object. The File or directory description can be obtained through the method provided by the File class, this mainly includes the name, path, readability, writability, and file length. It can also generate new directories, change file names, delete files, and list all files in a directory.

 
  1. Public static void main (String [] args) throws IOException {
  2. File f = new File ("dir ");
  3. F. createNewFile (); // create a. txt file.
  4. F. mkdir (); // create a directory named .txt
  5. /*
  6. * Use absolute path
  7. *
  8. * File f = new File ("D: \ dir \ src \ A. java ");
  9. *
  10. * F. createNewFile ();
  11. */
  12. /*
  13. * Cross-platform use
  14. *
  15. * Obtain the corresponding delimiter File fDir = new File (File. separator) based on different operating systems );
  16. *
  17. * String strFile = "dir" + File. separator + "src" + File. separator + "A. java ";
  18. *
  19. * File f = new File (fDir, strFile );
  20. *
  21. * F. createNewFile ();
  22. *
  23. * F. delete (); // delete a file or directory
  24. *
  25. * // F. deleteOnExit ();
  26. */
  27. /*
  28. * Create a temporary file in the default temporary file directory
  29. *
  30. * For (int I = 0; I <5; I ++)
  31. *
  32. *{
  33. *
  34. * File f = File. createTempFile ("winTemp", ". tmp ");
  35. *
  36. * F. deleteOnExit (); // Delete when exiting
  37. *
  38. *
  39. *
  40. *}
  41. */
  42. /*
  43. * List all subdirectories and file names in a specified directory
  44. */
  45. File fDir = new File (File. separator );
  46. String strFile = "dir" + File. separator + "src ";
  47. File f = new File (fDir, strFile );
  48. String [] names = f. list ();
  49. For (int I = 0; I <names. length; I ++ ){
  50. System. out. println (names [I]);
  51. }
  52. // If a filter exists, FilenameFilter is an interface.
  53. File dir = new File (File. separator );
  54. String filepath = "dir" + File. separator + "src ";
  55. /**
  56. * Dir
  57. * The upper-level abstract path. If dir is null, the program automatically calls the File constructor of a single parameter and applies the filepath path to the File but the constructor.
  58. * If dir is //, the path is the root directory of the disk where the file is located.
  59. */
  60. File f = new File (dir, filepath );
  61. If (f. exists ()){
  62. } Else {
  63. F. mkdirs ();
  64. }
  65. String [] names = f. list (new FilenameFilter () {// implements the FilenameFilter interface's Anonymous class, and implements the accept method to filter files
  66. @ Override
  67. Public boolean accept (File dir, String name ){
  68. System. out. println (name. indexOf (". java "));
  69. Return name. indexOf (". java ")! =-1;
  70. }
  71. });
  72. For (int I = 0; I <names. length; I ++ ){
  73. System. out. println (names [I]);
  74. }
  75. }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.