CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Apr 2009
    Posts
    26

    [RESOLVED] Java Program Copy Files

    I need to create a program that copies files from a directory to another directory. Then the copied files in the destination directory are to be renamed to their SHA1 hash code. Also, at the same time a new text file is created in the destination directory that contains the file names of the copied files with their SHA1 has code.
    Right now I copying contents from the source directory to the destination directory works.
    I am really confused and need help.
    Here's what I have so far:

    Code:
    import java.io.*;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class Main
    {
        public static void main(final String[] args)throws IOException
        {
        	Main cd = new Main();
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Enter the source directory or file name : ");
    		String source = in.readLine();
    		File src = new File(source);
    		System.out.println("Enter the destination directory or file name : ");
    		String destination = in.readLine();
    		File dst = new File(destination);
    		cd.copyDirectory(src, dst);
    
            File f;
            f = new File("manifest.txt");
            if(!f.exists())
            {
                f.createNewFile();
            }
    
    	}
    
    	public void copyDirectory(File srcPath, File dstPath) throws IOException{
    		if (srcPath.isDirectory())
    		{
    			if (!dstPath.exists())
    			{
    				dstPath.mkdir();
    			}
    
    			String files[] = srcPath.list();
    			for(int i = 0; i < files.length; i++)
    			{
    				copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
    			}
    		}
    		else
    		{
    			if(!srcPath.exists())
    			{
    				System.out.println("File or directory does not exist.");
    				System.exit(0);
    			}
    			else
    			{
    				InputStream in = new FileInputStream(srcPath);
    		        OutputStream out = new FileOutputStream(dstPath);
    
    				// Transfer bytes from in to out
    		        byte[] buf = new byte[1024];
    				int len;
    		        while ((len = in.read(buf)) > 0) {
    					out.write(buf, 0, len);
    				}
    				in.close();
    		        out.close();
    			}
    		}
    		System.out.println("Directory copied.");
    	}
    }
    Last edited by Backslash; April 14th, 2009 at 11:34 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured