This program will generate and put a password into a CSV file if it detects a field called 'Password' in the header. The example below will explain it better! Note that this only generates simple random passwords and should not be used for anything serious as is!
To keep it friendly and to mirror the Python version, most variable declarations are set to 'var', this lets the compiler figure out what type they should be. For more details on how this program works, check out the Java Programming Guides section!
import java.io.*;
import java.util.Random;
class Passgen{
static Random rand = new Random();
// The value passed is the length of the password
public static String pwgen(final int c){
// Removed vowels and similar looking characters!
final var alpha="bcdfghjkpqrstvwxyz23456789";
var pw="";
for(var i=0;i<c;i++){
var pos=rand.nextInt(alpha.length());
pw+=alpha.charAt(pos);
}
return pw;
}
public static void main(String[] args){
// Get the csv file name
var fn="";
if (args.length==1){
fn=args[0];
}else{
System.out.println("Usage: java Passgen myfile.csv");
return;
}
File myfile = new File(fn);
if (myfile.isFile()){
try(var fr=new FileReader(myfile); var in = new BufferedReader(fr)) {
String line;
var skip=true;
var pwpos=-1;
while((line = in.readLine()) != null){
var fields=line.split(",");
// Find password field position
if (skip==true){
for(var f=0;f<fields.length;f++){
if (fields[f].equals("Password")){
pwpos=f;
break;
}
}
if (pwpos==-1){
System.out.println("Password field not found?");
return;
}
// Outputs the header row
System.out.println(line);
skip=false;
continue;
}
// Put password in empty field
fields[pwpos]=pwgen(6);
// Output row
for(var i=0;i<fields.length;i++){
System.out.print(fields[i]);
if (i!=fields.length-1){System.out.print(",");}
}
System.out.println();
}
}catch(IOException e){
System.out.println("There was a problem opening this file!");
}
}else{
System.out.println("The file does not exist?");
}
}
}
Save the above as something, passgen.java for example. As it is a single file Java program we can run it directly with the Java command without the need to compile it first! The example csv file looks like:
First,Last,Password,Eyes#skip fred,bloggs,,blue joe,bloggs,,green
Run the program as shown below against the above csv file generates:
#skip>java passgen.java data.csv First,Last,Password,Eyes fred,bloggs,7pq74x,blue joe,bloggs,6fvwdj,green
To create a new file with these passwords, redirect the output by running as 'java passgen.java data.csv >new.csv' or similar.
Created 31/10/2026