The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java IO (Java File - How to copy directory - recursive copy)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java IO (Java File - How to copy directory - recursive copy) Posted: Nov 23, 2016 2:37 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java IO (Java File - How to copy directory - recursive copy)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube : 
https://www.youtube.com/watch?v=cUxoLssRyAE&list=UUhwKlOVR041tngjerWxVccw

FileDemo.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileDemo
{
public static void main(String[] args) throws IOException
{
File srcDir = new File("D:/Work/java");
File destDir = new File("D:/work/javanew");

// Make sure srcDir exists
if (!srcDir.exists())
{
System.out.println("Directory does not exist.");
}
else
{
FileDemo fileDemo = new FileDemo();
fileDemo.copydir(srcDir, destDir);
System.out.println("Copied successfully.");
}

}

public void copydir(File src, File dest) throws IOException
{

if (src.isDirectory())
{

// if directory not exists, create it
if (!dest.exists())
{
dest.mkdir();
System.out.println("Directory copied from " + src + " to "
+ dest);
}

// list all the directory contents
String files[] = src.list();

for (String fileName : files)
{
// construct the src and dest file structure
File srcFile = new File(src, fileName);
File destFile = new File(dest, fileName);
// recursive copy
copydir(srcFile, destFile);
}

}
else
{
// If file, then copy it
fileCopy(src, dest);
}
}

private void fileCopy(File src, File dest)
throws FileNotFoundException, IOException
{

InputStream in = null;
OutputStream out = null;

try
{
// If file, then copy it
in = new FileInputStream(src);
out = new FileOutputStream(dest);

byte[] buffer = new byte[1024];

int length;
// Copy the file content in bytes
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
}

}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
System.out.println("File copied from " + src + " to " + dest);
}
}
Output
Directory copied from D:\Work\java  to D:\work\javanew
File copied from D:\Work\java\index.html to D:\work\javanew\index.html
----
----
File copied from D:\Work\java\websocket-deprecated\index.html to D:\work\javanew\websocket-deprecated\index.html
File copied from D:\Work\java\websocket-deprecated\snake.html to D:\work\javanew\websocket-deprecated\snake.html
Copied successfully.

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_File_copy_dir_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_File_copy_dir_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/a3d05269db9922e504bb1f9de8d07031990c9910/BasicJava/JavaIODemo_File_copy_dir_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Tutorial : Java IO (Java File - How to copy directory - recursive copy)

    Topic: Google's Go language takes on compilation speed Previous Topic   Next Topic Topic: Inheriting Javadoc Method Comments

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use