Home >Java >javaTutorial >Java Example - File Renaming

Java Example - File Renaming

黄舟
黄舟Original
2017-02-15 10:18:351622browse

The following example demonstrates the use of the oldName.renameTo(newName) method of the File class to rename a file:

/*
 author by w3cschool.cc
 Main.java
 */import java.io.File;public class Main {
   public static void main(String[] args) {
      File oldName = new File("C:/program.txt");
      File newName = new File("C:/java.txt");
      if(oldName.renameTo(newName)) {
         System.out.println("已重命名");
      } else {
         System.out.println("Error");
      }
   }}

The output result of the above code is (before executing the program, you can Create program.txt file):

已重命名

The above is the Java example-file renaming content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn