The file class of Java

Source: Internet
Author: User

The file class is an abstract representation of files and directory names in Java. The file class can be used to create, delete, rename, get paths, create time, and so on, and is the only operation related to the file itself. Therefore, it is necessary to study the file class well.

main methods of use:1.public file (String pathname)----Construction of a document instance based on its path2.public Boolean CreateNewFile () throws IOException-----Create file3.public Boolean Delete ()----------Delete file4.public String getParent ()----------get the top-level path to the file5.public boolean isdirectory ()------Determine if the folder is not6.public boolean isfile ()------Determine if the file is not7.public string[]list ()------------lists the file names in the folder8.public file[] listfile ()------------list all file instances of a folder9.public boolean mkdir ()----------Create a folder10.public boolean renameto (file dest)-----files renamed (can understand moving files)11.public Long Length ()-------Return file size12.String getpath ()-----------path name string

Example code:
  1. Package com;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. public class Test {
  5. public static void Main (string[] args) {
  6. File.separator is cross-platform, under Windows for D:\\1.txt,linux d:/1.txt
  7. File File=new file ("D:" +file.separator+ "1.txt");
  8. File does not exist
  9. if (!file.exists ())
  10. {
  11. try {
  12. if (File.createnewfile ())//Create File
  13. System.out.println ("File creation succeeded");
  14. } catch (IOException e) {
  15. E.printstacktrace ();
  16. }
  17. }
  18. Get the top-level path to the file
  19. SYSTEM.OUT.PRINTLN ("Parent directory:" +file.getparent ());
  20. Determine if it is a directory
  21. System.out.println ("is directory:" +file.isdirectory ());
  22. Determine if it is a file
  23. System.out.println ("Whether it is a file:" +file.isfile ());
  24. Get File size
  25. SYSTEM.OUT.PRINTLN ("File Size:" +file.length ());
  26. File path
  27. System.out.println (File.getpath ());
  28. Rename, when the return value is true. The equivalent of moving d:\\1.txt to l:\\kk22.php.
  29. if (File.renameto (New file ("l:\\kk22.php")))
  30. System.out.println ("renaming succeeded");
  31. else {
  32. System.out.println ("rename failed");
  33. }
  34. Point to renamed File
  35. File=new File ("l:\\kk22.php");
  36. if (File.delete ())
  37. SYSTEM.OUT.PRINTLN ("File deletion succeeded");
  38. --------------------------------------Catalog Demo
  39. File=new File ("L:" +file.separator+ "javatest");
  40. does not exist
  41. if (!file.exists ())
  42. {
  43. File.mkdir ();//Create the Directory
  44. }
  45. Add files to this directory
  46. String temp;
  47. for (int i=0;i<4;i++)
  48. {
  49. Temp= "";
  50. temp= "L:" +file.separator+ "javatest" +file.separator+i+ "TXT";
  51. File Ok=new file (temp);
  52. try {
  53. Ok.createnewfile ();//Create the file
  54. } catch (IOException e) {
  55. TODO auto-generated Catch block
  56. E.printstacktrace ();
  57. }//
  58. }
  59. Get the file name under this directory
  60. String[] Name=file.list ();
  61. for (int i=0;i<4;i++)
  62. System.out.println (Name[i]);
  63. Get a file instance under this directory
  64. File[] Files=file.listfiles ();
  65. for (int i=0;i<4;i++)
  66. System.out.println (Files[i].getpath ());
  67. }
  68. }
Copy CodeFile can only get the files path creation time and other information, as well as the creation and deletion of files, the contents of the file can not be obtained and modified. The Java IO Stream is required to obtain and modify the specific contents of the file. The IO-related operations are then explained.

The file class of Java

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.