/** This program was created to anonymize files/folders and produce a key file mapping random file names back to the original file names. **/ import java.io.*; import java.util.*; public class Anonymize{ /** * Main method that does all of the work. Usage: * * java Anonymize folderName keyFile.csv * * @param args[0]=folderName folder that contains the files/folders you wish to anonymize. * @param args[1]=keyFile.csv a csv file that is created that contains "New Filename","Original Filename" pairs. */ public static void main(String[] args) throws Exception{ if(args.length != 2){ System.out.println("Program usage is:\njava Anonymize nameOfFolder keyFile.csv"); System.exit(1); } File folderName = new File(args[0]); if(folderName.isDirectory()){ HashSet usedAlready = new HashSet<>(); PrintWriter fout = new PrintWriter(new FileWriter(args[1])); fout.println("New Name, Original Name"); Random random = new Random(); int numberFiles = folderName.listFiles().length; for(File current : folderName.listFiles()){ String name = current.getName(); String extension = ""; //get extension if it's a file instead of a folder if(name.contains(".")){ extension = name.substring(name.lastIndexOf(".")); } //pick a random number to change the folder/file name to int rename = random.nextInt(numberFiles*10); while(usedAlready.contains(rename)){ //increment the random number in the unlikely event of a collision rename = rename+1; } current.renameTo(new File(folderName+File.separator+Integer.toString(rename)+extension)); //store random number so it isn't used again usedAlready.add(rename); fout.println(Integer.toString(rename)+extension+","+name); } fout.close(); } else{ System.out.println("Folder required. Program usage is:\njava Anonymize nameOfFolder keyFile.csv"); System.exit(1); } } }