Home > Java > javaTutorial > Java Example - File Renaming

Java Example - File Renaming

黄舟
Release: 2017-02-15 10:18:35
Original
1602 people have browsed it

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");
      }
   }}
Copy after login

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

已重命名
Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template