Write a program to rename all .txt file in a directory?

devquora
devquora

Posted On: Dec 24, 2020

 

A program to rename all .txt file in a directory is as follows:

import java.io.File;
public class Demo {
   public static void main(String[] args) {
      try {
         File file1 = new File("demo1.txt");
         File file2 = new File("demo2.txt");
         file1.createNewFile();
         file2.createNewFile();
         boolean flag = file1.renameTo(file2);
         System.out.print("File renamed? " + flag);
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}



    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Amazon Support Engineer Interview Questions

    Explain What is STP?

    The STP also stands for Spanning Tree Protocol constructs a loop-free logical topography for Ethernet networks. It is a ..

    Amazon Support Engineer Interview Questions

    Write a function to check if a number is prime?

    A prime number is a whole number greater than 1, which is only divisible by 1 and itself. // function check whether a number // is prime or not bool isPrime(int n) { // Corner case if (n...

    Amazon Support Engineer Interview Questions

    Write a regular expression to validate phone number in  XXX-XXX-XXXX format?

    A regular expression to validate phone number in XXX-XXX-XXXX format is follows: /^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/...