0
import java.util.Random;
import java.io.*;

class Encrypt
{
 public static void main(String args[])
{
    try{
    String source=System.console().readLine("Enter name of file to be encrypted:- ");
    File f1=new File(source);
    File f2=new File("~temp");//create a new 
    FileInputStream fis=new FileInputStream(f1);
    FileOutputStream fos=new FileOutputStream(f2);
    int a=0;
    Random rand=new Random();
    int key=0;
    while(key==0)
        key=rand.nextInt(16);
    fos.write(key);
    boolean alt=true;
    while((a=fis.read())!=(-1))
    {
        if(alt)
            a=a+key;
        else
            a=a-key;
        fos.write(a);
        alt=!alt;
    }
    fos.flush();
    fis.close();
    fos.close();
    f1.delete();
    f2.renameTo(f1);
    }
    catch(Exception e)
    {
        System.out.print(e);
    }
}
}

this code is working fine with any type of file, but it may take more time when the file size is more. Though it will take more time for folder too but i need to do this. I want to pass the folder and encrypt every file in it, and if there is an another folder then the files in that folder should be encrypted. i want to do this for the contents in a folder. What will be the best way to do this?

That is how I run that loop:

 File folder = new File("source");
 File[] listOfFiles = folder.listFiles();
 for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    //my encryption method
  } else if (listOfFiles[i].isDirectory()) {
    //my encryption method
  }
}
Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
  • Do you want to encrypt the entire folder including the files or do you want to pass an Folder and encrypt every single file in it? – Iralution Jul 08 '14 at 14:25
  • I want to pass the folder and encrypt every file in it, and if there is an another folder then the files in that folder should be encrypted. –  Jul 08 '14 at 14:40
  • 2
    The best way IMO is to use a function recurssively: you give it a folder; it loops on the contents, if file: it encripts, if folder runs again in it. – ionutioio Jul 08 '14 at 14:48
  • I tried to run the loop but it throws an FileNotFound exception. Could you please tell me how can I do it? –  Jul 08 '14 at 14:52
  • Edit your question and show us how did you run the loop. It is better if you provide us your code, this way we can show you were you made your mistake, so you have something to learn, instead of just directly giving you the solution.. – Daniel Jul 08 '14 at 15:06
  • possible duplicate of [Recursively list files in Java](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) – Oleg Estekhin Jul 08 '14 at 15:48

1 Answers1

0

This traverese a file-tree or handles a single file and encrypts every single item

import java.io.File;

public class Encrypter {

    public static void main(String[] args) {
        Encrypter enc = new Encrypter();
        enc.traverseFileTree(new File("root directory or single File"));
    }

    private void traverseFileTree(File node) {
        if (node.isDirectory()) {
            for (String child : node.list()) {
                traverseFileTree(new File(node, child));
            }

            if (node.isFile()) {
                encrypt(node);
            }
        }
    }

    public void encrypt(File file) {

        // your encryption method
    }

}
Iralution
  • 480
  • 1
  • 5
  • 16