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.









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 copy files from one folder to another based on lastmodified date - Java Beginners
Copy files from one folder to another  How to copy files from one folder to another based on lastmodified date
How to copy files from one folder to another based on lastmodified date - Java Beginners
How to copy files from one folder to another based on lastmodified date  I have to make a simple utility to copy files from a folder to another folder in the same hierarchy, without overwriting the existing.  hiimport
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
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
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
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
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
PHP listing files in folder
All files, folder   of the given folder can be listed. Function readdir() takes input as the folder handle. Then readdir() returns the name of all the files and folders. Example of PHP Listing Files in 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
How I Choose a folder - Java Interview Questions
do not explain clearly my question. I know how I get files from a folder. My question How I select a folder. From this selected folder i get the files. I know how I get files but the problem How I choose a folder. Thanks
Monitor the Folder for the new incoming files - Development process
Monitor the Folder for the new incoming files  to monitor the whole folder for new incoming files ,which the client will place...Monitor the Folder for the new incoming files   Hi,The simplest way to achieve
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 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  
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 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
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 all files of folders. The list() method is used to find list of files in a folder
How to upload and save a file in a folder - Java Beginners
How to upload and save a file in a folder  Hi How to upload and save a file in a folder? for example Agreement.jsp File: So when..., Thanks for your code.... Do i need any jarfiles to be placed in lib? Java
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
Zipping folder - Java Beginners
to zip a user selected folder . this folder may contain any number of folder and sub folders. i saw an example in this site showing , how to zip a folder, i executed... solve this.. zipping folder and all of its files and sub folders. thanking
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
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. 
copy file from folder to folder - Java Beginners
copy file from folder to folder   my requirement is I need to copy xml files from one folder to another my source file contains the field filename.... Suppose in the source file, file name is goodfile1.xml source folder
how to set folder size in php
how to set folder size in php  hi , i know how to create folder but i dont know how to set size for folder did u know means tell me. thankyour
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
Zipping folder - Java Beginners
to zip a user selected folder . this folder may contain any number of folder and sub folders. i saw an example in this site showing , how to zip a folder, i executed... solve this.. zipping folder and all of its files and sub folders. thanking
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
Creat a folder, inside it upload and download files in jsp and mysql
Creat a folder, inside it upload and download files in jsp and mysql  Create one or more folder .inside it we can upload and download multiple files... to database. The uploaded files will then displayed on the browser and will allow the user
image save to folder in java
image save to folder in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open... folder and that path stored into database, for furhter retrivation. Please guide me
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
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 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
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
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... through which we can check if a folder exists or not in Java. A new Java
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
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 get Folder Size
Java get Folder Size      ... of specified folder and also the number of files and folders that are presented... to get Folder Size in Java?": folder.getName()-This method returns the folder
How to copy an jpeg image from one folder to other folder using jsp
How to copy an jpeg image from one folder to other folder using jsp  How to copy an image from one folder to another folder using jsp
Java- Save Image into project folder in web application
Java- Save Image into project folder in web application  I have a requirement of saving the image into project folder .. which is a web application. I am not able to figure out how to find the absolute path of my folder which I
how to take iphone backup using itunes
how to take iphone backup using itunes  Hi, Can anyone please tell me how to take iphone backup using itunes..?   You can find the details on apple website as well... anyhow i am giving you some tips as follow... How
How to copy an jpeg image from one folder to other folder using jsp
How to copy an jpeg image from one folder to other folder using jsp  ... be in my image folder...so,How to copy an jpeg image from one folder to other folder using jsp or servlet
How to backup a selected file from the dropDown Menu in JSP?
How to backup a selected file from the dropDown Menu in JSP?  I am trying to create a dropdown menu list for some type of files contained ina... file into the backup directory. I need the jsp code that can generate
Copy Files - Java Beginners
Copy Files  I saw the post on copying multiple files (http://www.roseindia.net/java/example/java/io/CopyMultipleFiles.shtml) and I have something very similar, but I'm really not sure how I would go about doing it. I have

Ads