Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to rename file in Java

2 Answers

0 votes
import java.io.File;

class program {
    public static void main(String[] args) {
        try {
            File file_old = new File("d:\\data.bin");
            File file_new = new File("d:\\data_new_name.bin");
                      
            if (file_new.exists()) {
                throw new java.io.IOException("d:\\data_new_name.bin - exists");
            }
          
            boolean success = file_old.renameTo(file_new);
                      
            if (!success) {
                System.out.println("Rename error");
            } else {
                System.out.println("Rename success");
            }
        }    
        catch (Exception e) {
            System.out.println("Error rename file: " + e.getMessage());   
        }
    }
}
 
   
/*
run:
   
Rename success
   
*/

 



answered Aug 26, 2015 by avibootz
edited Apr 23, 2024 by avibootz
0 votes
import java.io.File;

class program {
    public static void main(String[] args) {
        try {
            File oldName = new File("d://data.txt");
            File newName = new File("d://data-old.txt");
 
            boolean b = oldName.renameTo(newName);
 
            if (b) {
                System.out.println("File renamed");
            } else {
                System.out.println("File not renamed");
            }
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

  
/*
run:
      
File renamed
   
*/

 



answered Apr 23, 2024 by avibootz

Related questions

1 answer 134 views
134 views asked Nov 19, 2016 by avibootz
1 answer 107 views
107 views asked Aug 22, 2023 by avibootz
1 answer 159 views
159 views asked Jul 12, 2020 by avibootz
1 answer 213 views
213 views asked Mar 10, 2020 by avibootz
1 answer 229 views
1 answer 198 views
198 views asked Jun 9, 2018 by avibootz
1 answer 206 views
206 views asked Aug 26, 2015 by avibootz
...