[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.");
}
}
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
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)
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]
Re: Java Program Copy Files
Quote:
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...
Re: Java Program Copy Files
Quote:
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.
Re: Java Program Copy Files
Quote:
Originally Posted by
postmortem
Thank You!