Skip to content

Instantly share code, notes, and snippets.

@JoshCode
Last active August 29, 2015 14:21
Show Gist options
  • Save JoshCode/87e372caf5aad1f8b5d2 to your computer and use it in GitHub Desktop.
Save JoshCode/87e372caf5aad1f8b5d2 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author <a href="http://www.joshuaslik.nl/" target="_blank">Joshua Slik</a>
*/
public class Main {
private static String filename = "Words";
private static int wordnumber = 171476; // Estimate of the number of different words currently in use in the Oxford dictionary
private static SecureRandom random = new SecureRandom();
public static void main(String[] args) throws InterruptedException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
long timeStart, timeStop, timeDiff;
System.out.println("Time at start of file generation : " + sdf.format(new Date()));
timeStart = System.nanoTime();
makeFile();
timeStop = System.nanoTime();
timeDiff = timeStop - timeStart;
System.out.println("Time at end of file generation : " + sdf.format(new Date()));
System.out.println("Time elapsed during generation : " +
timeDiff + " nanoseconds / " +
Math.round((timeDiff / 1e6) * 1000.0) / 1000.0 + " milliseconds / " +
Math.round((timeDiff / 1e9) * 1000.0) / 1000.0 + " seconds.");
System.out.println("Waiting a bit");
Thread.sleep(3000);
System.out.println("Time at start of file iteration : " + sdf.format(new Date()));
timeStart = System.nanoTime();
iterate();
timeStop = System.nanoTime();
timeDiff = timeStop - timeStart;
System.out.println("Time at end of file iteration : " + sdf.format(new Date()));
System.out.println("Time elapsed during iteration : " +
timeDiff + " nanoseconds / " +
Math.round((timeDiff / 1e6) * 1000.0) / 1000.0 + " milliseconds / " +
Math.round((timeDiff / 1e9) * 1000.0) / 1000.0 + " seconds.");
}
public static void makeFile() {
File output = new File(filename);
FileWriter fw = null;
try {
fw = new FileWriter(output);
} catch (IOException e) {
e.printStackTrace();
}
if (fw == null) {
return;
}
for (int i = 0; i <= wordnumber; i++) {
try {
fw.write(new BigInteger(32, random).toString(32));
fw.write(String.format("%n"));
//System.out.println("Printed " + i);
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(i);
}
try {
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void iterate() {
File input = new File(filename);
File output = new File(filename + " copy");
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter(output);
} catch (IOException e) {
e.printStackTrace();
}
if (fw == null) {
return;
}
try {
br = new BufferedReader(new FileReader(input));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (br == null) {
return;
}
try {
String line;
while ((line = br.readLine()) != null) {
try {
fw.write(line);
fw.write(String.format("%n"));
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment