Java obtains all file names in a specified folder.

Source: Internet
Author: User
  1. Package


    Test;


  2. Import


    Java. Io. file;


  3. /**

     
  4. * @ Author yinxm

     

  5. * @ Version 1.0

     

  6. *

     

  7. * This class can take file's path and file's name;

     

  8. * You must give the path where you want to take the file.

     

  9. */



  10. Public


     

    Class


    Takefilepathandname {




  11. Public


     

    Static


     

    Void


    Main (string [] ARGs ){



  12. // This is the path where the file's name you want to take.


  13. String Path =

    "C: // Documents and Settings // yinxm // your website has been created successfully. // testfile"

    ;

  14. GetFile (PATH );

  15. }




  16. Private


     

    Static


     

    Void


    GetFile (string path ){



  17. // Get file list where the path has


  18. File file =

    New


    File (PATH );



  19. // Get the folder list


  20. File [] array = file. listfiles ();




  21. For


    (

    Int


    I =
    0
    ; I <array. length; I ++ ){



  22. If


    (Array [I]. isfile ()){



  23. // Only take file name


  24. System. Out. println (

    "^"

    + Array [I]. getname ());



  25. // Take file path and name


  26. System. Out. println (

    "#####"

    + Array [I]);



  27. // Take file path and name


  28. System. Out. println (

    "*****"

    + Array [I]. getpath ());

  29. }

    Else


     

    If


    (Array [I]. isdirectory ()){

  30. GetFile (array [I]. getpath ());

  31. }

  32. }

  33. }

  34. }

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.