Java implements simple method analysis to modify file names

  • 2020-09-16 07:30:20
  • OfStack

This article illustrates how Java implements simple filename changes. To share for your reference, the details are as follows:

Today to help friends some websites, do goods upload when the need to set the name of the file again, before also did the function of the class, but did not save forgotten, in order to avoid looking for again later, in this record, ha ha...

Example 1:


import java.io.*;
public class test1
{
 public static void main(String[] args) {
  File file=new File("D:/gai.jpg"); // Specifies the file name and path 
  String name="123";
  String filename=file.getAbsolutePath();
  if(filename.indexOf(".")>=0)
  {
   filename = filename.substring(0,filename.lastIndexOf("."));
  }
  file.renameTo(new File(name+".jpg")); // The name 
 }
}

Example 2:


public class xfile {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO  Automatically generate method stubs 
  File f=new File("d:/old.txt");
  String c=f.getParent();
  File mm=new File(c+File.pathSeparator+"new.txt");
  if(f.renameTo(mm))
  {
  System.out.println(" Modify the success !");
  }
  else
  {
  System.out.println(" Modify the failure ");
  }
 }
}

For more information about java algorithm, please refer to Java File and directory Operation Skills Summary, Java Data Structure and Algorithm Tutorial, Java Operation Skills Summary of DOM Node and Java Cache Operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: