Method Example of Java Realizing Batch Modification of txt File Names

  • 2021-07-01 07:37:59
  • OfStack

In this paper, an example is given to describe the method of modifying txt file names in batches by Java. Share it for your reference, as follows:

Recently, when doing the experiment of feature selection, it is necessary to modify the file name under 1 batch, and make 1 record here.


package com.cqu.experiment;
import java.io.File;
/**
 * @author  By:  E-mail:@126.com
 * @version  Creation time: 2016 Year 12 Month 26 Day   Afternoon 3:02:01  Class description 
 */
public class RenameTxt
{
    public static void renameTxt(File file)
    {
        if (file.isDirectory())
        {
            File[] files = file.listFiles();
            System.out.println(files.length);
            for (File f : files)
            {
                System.out.println(f.getName());
                String originalName = f.getName();
                System.out.println(originalName);
                String newName = "10" + originalName;
                String newFilePath = "F:\\Develop_Code\\workspace\\Research\\3. Feature selection \\TrainingSet\\C000010";
                File newFileName = new File(newFilePath + "\\" + newName);
                synchronized(f)
                {
                    f.renameTo(newFileName);
                }
            }
        }
    }
}


package com.cqu.experiment;
import java.io.File;
/**
* @author  By:  E-mail:@126.com
* @version  Creation time: 2016 Year 12 Month 26 Day   Afternoon 3:01:46
*  Class description 
*/
public class Main
{
    public static void main(String[] args)
    {
        String dir = "F:\\Develop_Code\\workspace\\Research\\C000010";
        File file = new File(dir);
        File[] files = file.listFiles();
        RenameTxt.renameTxt(file);
    }
}

More readers interested in java algorithm can check the topics of this site: "Summary of Java File and Directory Operation Skills", "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: