how to backup files and folder with Java

how to backup files and folder with Java

Does anyone know to create a backup with java?. backup files and folders from my computer to an external drive.

View Answers

May 30, 2013 at 1:27 PM

hi friend,

Try the following code may, this will be helpful for you

package net.roseindia;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class CreateBackupFile{
    public void createBackup(String filename, String... content)
                        throws IOException{
        File originalFile = new File(filename);     
        File backupFile = new File(originalFile.getCanonicalPath() + ".backup");

        originalFile.renameTo(backupFile);
        PrintWriter output = new PrintWriter(originalFile);
        for(String fileData : content){
            output.println(fileData);
        }
        output.close();
    }

    public static void main(String args[])
    {
        CreateBackupFile create = new CreateBackupFile();
        String fileName = "C:\\Documents and Settings\\bharat\\Desktop\\bipul\\New Folder\\TableExample.java";
        File file = new File(fileName);
        BufferedReader br = null;
        String str ="";     

        try {
            br = new BufferedReader(new FileReader(file));
            StringBuilder sb = new StringBuilder();
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            String str1 = sb.toString();            
            create.createBackup(fileName, str1);
            System.out.println(str1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Thanks.


May 31, 2013 at 6:24 AM

thanks, very nice code.

Ads









Related Tutorials/Questions & Answers:
how to backup files and folder with Java
how to backup files and folder with Java  Does anyone know to create a backup with java?. backup files and folders from my computer to an external...(originalFile.getCanonicalPath() + ".backup"); originalFile.renameTo
how to download the uploaded folder files using jsp
how to download the uploaded folder files using jsp  how to download the uploaded folder files using jsp
Advertisements
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix  Hi, I am using ubuntu and my need is to find all the files containing a text say "India". How... terminal to find a text in all the files in all directories? how to search
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix  Hi, I am using ubuntu and my need is to find all the files containing a text say "India". How... terminal to find a text in all the files in all directories? how to search
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix  Hi, I am using ubuntu and my need is to find all the files containing a text say "India". How... terminal to find a text in all the files in all directories? how to search
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix  Hi, I am using ubuntu and my need is to find all the files containing a text say "India". How... terminal to find a text in all the files in all directories? how to search
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?  i have multiple(app..100) text files which has data in the form of intgers consisting of 3 columns and app..3000
files that are present in any folder , how to make it clickable downloadble file in php
files that are present in any folder , how to make it clickable downloadble file in php   files that are present in any folder , how to make it clickable downloadble file in php
java program to take the backup of folders and copy the folder in different location without deleting the existing one
java program to take the backup of folders and copy the folder in different... the backup of folders and copy the folder in different location without deleting the existing one   Have a look at the following link: Java Copy one folder
how to upload multiple files in jsp and saving the path in database and the file in folder
how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path in database and the file in folder I have created a form for the upload of files
Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.
Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.   import java.io.File; import... the method a very static scenario. I want to take the files inside the folder
Folder in Java.
Folder in Java.  I have any problem how to make Folder in java program? Can Anybody help me in this part
ModuleNotFoundError: No module named 'backwork-backup-files'
: No module named 'backwork-backup-files' How to remove the ModuleNotFoundError: No module named 'backwork-backup-files' error? Thanks   Hi...ModuleNotFoundError: No module named 'backwork-backup-files'  Hi
how to backup and restore database using java - Swing AWT
how to backup and restore database using java  sir my project is to backup and restore my datas using java so i want to know how to backup and restore databases using java give ur ideas and suggestions to me immediately  
Zip Folder Using Java
; In this example we are going to create a zip folder and all files contained in this folder are zipped by using java. Here we will learn how to compress files... of folders. The list() method is used to find list of files in a folder. By using
How to delete files in Java?
Delete a File using File Class Object       Delete a File using File Class Object In this section, you will learn how to delete a file. 
how to take backup of phone to pc using java code
how to take backup of phone to pc using java code  i want to take the backup of phone data in my pc and also provide the restore functionality,and i want to do it using java program,so can anyone help me
java program to take backup for the folders
java program to take backup for the folders  java program to take backup for the folders   Have a look at the following link: Java Copy one folder to another
how to download a file from a folder??
how to download a file from a folder??  i can upload any kind of files to a folder, and only the path of the file is saved into the database, now how a client can download the file from my folder. pls provide me the jsp code
Java backup pst file
Java backup pst file  Hello!! i need some help please. Im new in Java. How can i create a java program to backup any files and folders (like PST files)in my computer at a specific time
how to take a backup of a table in sql
how to take a backup of a table in sql  how to take a backup of a table in sql?   SQL BackUp Table example
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive multiple files from a remote URL as for (String reportid : reportList...[]args){ CreateJar jar=new CreateJar(); File folder = new File("C
how to copy files from remote location to local using java?
how to copy files from remote location to local using java?  I want to copy all files from shared folder to local. Please help
How to make a folder icon, make a folder icon, folder icon
How to make a folder icon       The folder is used in the computer, this example will teach you to design technique of the folder.  New File: Take a new file as your folder
How do I decompile Java class files?
How do I decompile Java class files?  How do I decompile Java class files
How to avoid Java Code in JSP-Files?
How to avoid Java Code in JSP-Files?  How to avoid Java Code in JSP-Files
Java lock file or folder.
Java lock file or folder.  Is it possible to create a desktop application to lock or encrypt file or folder in windows by using java code?? if possible can provide some reference?? because i can't find it... thanks
how to browse folder and directory in jsp
how to browse folder and directory in jsp  how to browse folder and directory in my jsp page.i have done my coding part for browsing directory but dont know how to get files after that.and also i want to show limited directories
Video Tutorial: How to check if a folder exists in Java through a source code
How to check if a folder exists in Java through a source code Java is an all... facilities. Here is the video tutorial of "How to check if a folder exists in Java... can check if a folder exists or not in Java. A new Java project is created
How to run java swing application from jar files .
How to run java swing application from jar files .  Hello Sir, I developed a java swing application .Now i want to execute it as .exe setup file .As per my knowledge i have to run .jar file from dist folder
java program to copy the contents of one folder to another folder
java program to copy the contents of one folder to another folder  java program to copy the contents of one folder to another folder   Have a look at the following link: Java Copy one folder to another

Ads