|
-
April 14th, 2009, 11:13 PM
#1
[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.
-
April 15th, 2009, 05:28 AM
#2
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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 16th, 2009, 09:40 PM
#3
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)
-
April 17th, 2009, 12:26 AM
#4
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]
-
April 17th, 2009, 12:39 AM
#5
Re: Java Program Copy Files
 Originally Posted by postmortem
[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...
-
April 17th, 2009, 12:43 AM
#6
Re: Java Program Copy Files
 Originally Posted by Backslash
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.
-
April 17th, 2009, 04:46 AM
#7
Re: Java Program Copy Files
 Originally Posted by postmortem
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|