1

I am trying to write a code that just changes the file name without reading and writing it with a different name, is there any built in function for that? I tried to search for

and if not, the best way would be rewriting it with a new name?

Maroun
  • 94,125
  • 30
  • 188
  • 241
user3256396
  • 73
  • 1
  • 3
  • 1
    This question appears to be off-topic because it doesn't demonstrate a minimal understanding of the problem being solved. – Maroun Jan 31 '14 at 11:37
  • sorry gor the idiotic question, i was looking for a renameTo function – user3256396 Jan 31 '14 at 11:41
  • That's not idiotic question. It's a valid question, but a minimal research efforts would have gave you the answer quickly. – Maroun Jan 31 '14 at 11:42
  • i did research, but because of ,y not very good english i didnt find an answer, ill be glad if someone that can, will edit my question so it would be better. – user3256396 Jan 31 '14 at 11:45
  • [Welcome to SO](http://stackoverflow.com/tour), I'm sure you'll be involved in helpful topics here. – Maroun Jan 31 '14 at 11:46

3 Answers3

5

How about the File.renameTo(File dest) function?

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
0

Try:

new File("MyFile.txt").renameTo(new File("MyNewFile.txt"));
PureGero
  • 937
  • 2
  • 8
  • 16
0

renameTo

public boolean renameTo(File dest)

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

Parameters: dest - The new abstract pathname for the named file

Returns: true if and only if the renaming succeeded; false otherwise

Throws: SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to either the old or new pathnames NullPointerException - If parameter dest is null

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132