CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  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.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Program Copy Files

    <sigh> So what is your question? Explain what you are confused about and exactly what you need help with.

    You may find this useful: How To Ask Questions the Smart Way.

    Vague and nebulous is the beginning of all things, but not their end...
    K. Gibran
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2009
    Posts
    26

    Re: Java Program Copy Files

    I need help on how to generate SHA1 hash code and rename it from the copied file names.
    I also need help on how to paste these filenames that I copied into a txt file with their SHA1 hash code. (in my code it's manifest.txt)

  4. #4
    Join Date
    Oct 2008
    Posts
    77

    Re: Java Program Copy Files

    [sarcasm] well all you need to find another piece of code online and combine with one "that you got" [/sarcarsm]

  5. #5
    Join Date
    Apr 2009
    Posts
    26

    Re: Java Program Copy Files

    Quote Originally Posted by postmortem View Post
    [sarcasm] well all you need to find another piece of code online and combine with one "that you got" [/sarcarsm]
    i asked for help with not an opinion...

  6. #6
    Join Date
    Oct 2008
    Posts
    77

    Re: Java Program Copy Files

    Quote Originally Posted by Backslash View Post
    i asked for help with not an opinion...
    seriously,
    this should be enough:
    http://en.wikipedia.org/wiki/SHA1#SHA-1_pseudocode

    you have pseudo code, it is fairly trivial to write code from it; if it is not for you, then you'll learn a lot during the process of conversion to real code.

  7. #7
    Join Date
    Apr 2009
    Posts
    26

    Re: Java Program Copy Files

    Quote Originally Posted by postmortem View Post
    seriously,
    this should be enough:
    http://en.wikipedia.org/wiki/SHA1#SHA-1_pseudocode

    you have pseudo code, it is fairly trivial to write code from it; if it is not for you, then you'll learn a lot during the process of conversion to real code.
    Thank You!

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