Question: Java Programming I am trying to run below program in NetBeans and it is showing the following error: Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 0 at

Java Programming

I am trying to run below program in NetBeans and it is showing the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at RenameFile.main(RenameFile.java:22)

Please fix the code for NetBeans.

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class RenameFile {

public static void main(String[] args) throws IOException {

// File (or directory) with old name

File file = new File(args[0]);

String pattern = "E MMM dd HH_mm_ss z yyyy";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String date = simpleDateFormat.format(new Date());

// File (or directory) with new name

if (!file.exists()) {

System.out.println("The file does not exist");

} else {

File file2 = new File(args[0] + date);

// Rename file (or directory)

boolean success = file.renameTo(file2);

if (!success) {

// File was not successfully renamed

System.out.println("Error while remaining the file");

} else {

System.out.println("Success. New file is " + file2.toPath());

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!