Read all the file names of subfolders under the folder and save them as txt files named with __java

Source: Internet
Author: User

Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileWriter;
 
Import java.io.IOException;   
        public class Takefilepathandname {public static void main (string[] args) throws filenotfoundexception{   
        This is the path where the file ' s name you want to take.
        String path = "D:\\10stanford 100";
        File File = new file (path);
        file[] array = File.listfiles ();
        String s = null;
        		  for (int i =0;i< array.length;i++) {try {s = "d:\\stanford\\" +array[i].getname () + ". txt";
	 				File File1 = new file (s);
	 			File1.createnewfile ();
	 			catch (IOException e) {//TODO automatically generated catch block E.printstacktrace (); } getFile (Array[i].getpath (), s); Gets the file path under the root directory} private static void GetFile (String path,string s) {//Get file list wh 
  Ere the path has//Get the folder List file File1 = new file (path);      file[] array = File1.listfiles ();
            try {FileWriter output = new FileWriter (s); for (int i=0;i<array.length;i++) {if (Array[i].isfile ()) {//system.out.println (array[i].getn
				   Ame ());
		   Output.append (Array[i].getname ());//Add in file (picture) name} output.close ();
		catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();   
 }
      }   
    }

because of some needs, you need to read all the file names of subfolders under the folder and save them in txt format as a file name


Input:


Output:





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.