Java Programming Tutorial #38 - How to Rename a File in Java

Sharing buttons:

hello all welcome to 38 Java programming

tutorial on it cycles calm my name is

Aditya and in this tutorial we will

learn how to rename a files so let's

create a file type object file followed

by file 1 or you can say that it is a

new file followed by the new keyword

file and as you know hidden its

parameter we need to pass the file name

that we want to create or you want to

perform certain operation on that so I

am creating added theodor txt which I

have already created if you drive to my

folder and as you can see that they are

dot txt is already in there so what we

are going to do now is we are going to

rename our they tear dot txt with test

dot txt so let's run how let's learn how

to do that so we need to create another

file type object and let's name is we

run file followed by new file and within

its parameter we need to pass the new

name which you want to assign to your

file so I am creating test dot txt and

within the conditional if statement we

need to write the new file dot rename to

and within its parameter we need to pass

the rename file object that's it so what

rename to do it will take a parameter

rename file which is the destination

file in which we want to convert and

before that we need to supply on

file object you want to perform that

operation so it will rename the auditor

dot txt to test dot txt

so let's inform the user that file pre

name successfully

fine we need or to make it better we can

simply write as new Curie new file dot

cat name as I told you earlier in my

previous tutorial that get name will

print the name of the file so let's

concatenate with it with renamed and

renamed followed by pen file dot get

name so this will look more appropriate

now if this code block doesn't execute

let's create the else part for love is

so the else part will print system dot

out dot println followed by operation

field which means either file not found

or there is some other type of error in

that so that's how we can rename the

file so let's compile and run our code

and check whether we are right or not so

to compile let's write Java C followed

by file handling seven for Java so as

you can see we get an error that rename

dot file get name ok sorry I did a

mistake that gate name should be written

like some method so let's compile our

code end okay now compilation is

successful now let's run our code so to

run let's write Java followed by file

pending seven so aditya dot txt rhenium

to to texture dot txt let's check back

to a folder as you can see X dot txt is

a renamed by editor tasty so you can see

however the content is not been erased

in our previous edited or txt hello

world was written how

when we rename did it the text inside

that do not change however the only the

front-end name all these of that file

changed so that's how we can rename the

file so you can create as many programs

like this and you can think currently

with more and if you have any

difficulties or doubt please know let me

ask in the youtube comment section and

until then enjoy your life thanks for