I haven't used enum so much, so I made it in practice. Practicality is unknown. The java version is 6.
SamleFixedLengthFromCsv.java
package prottype;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import au.com.bytecode.opencsv.CSVReader;
public class SamleFixedLengthFromCsv {
//Record definition: number of characters
enum recordDef {
col01(8), col02(24), col03(24), col04(3);
private int length;
private recordDef(int length) {
this.length = length;
}
public int getLength() {
return this.length;
}
}
//Record definition:(Default character,padding character)
enum recordVal {
col01("0,0"), col02(", "), col03(", "), col04("---,-");
private recordVal(String val) {
this.val = val;
}
private String val;
public void setVal(String val) {
this.val = val;
}
public String getVal() {
return this.val;
}
}
/**
*main class
*/
public static void main(String[] args) throws IOException {
SamleFixedLengthFromCsv proc = new SamleFixedLengthFromCsv();
proc.execute();
}
/**
*Run
*/
protected void execute() {
try {
mainProc();
} catch (Exception e) {
System.out.println("File output failed ...");
}
}
/**
*Main process
*/
public void mainProc() throws Exception {
PrintWriter pw = null;
CSVReader reader = null;
try {
File file = new File("./test.dat");
reader = new CSVReader(new FileReader(new File("./test.csv")));
file.delete();
file.createNewFile();
pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
String[] record;
while ((record = reader.readNext()) != null) {
recordVal.col01.setVal(record[0] + ",0");
recordVal.col02.setVal(record[1]+", ");
recordVal.col03.setVal(record[2]+", ");
recordVal.col04.setVal(record[3]+ ",-");
pw.print(createFixedRecords() + System.getProperty("line.separator") );
}
pw.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (reader != null) {
reader.close();
}
}
}
/**
*Fixed-length record creation
*/
private String createFixedRecords() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < recordVal.values().length; i++) {
sb.append(createFixedColumn(i));
}
return sb.toString();
}
/**
*Convert items to fixed length
*/
private String createFixedColumn(int defNum) {
StringBuilder tempPad = new StringBuilder();
StringBuilder val = new StringBuilder(recordVal.values()[defNum].getVal().split(",")[0]);
StringBuilder pad = new StringBuilder(recordVal.values()[defNum].getVal().split(",")[1]);
if (val.length() < recordDef.values()[defNum].length) {
for (int i = val.length(); i < recordDef.values()[defNum].length; i++) {
tempPad.append(pad);
}
}
//Numbers are right-justified, otherwise left-justified
try {
Integer.parseInt(val.toString());
return tempPad.append(val).toString();
} catch(Exception e) {
return val.append(tempPad.toString()).toString();
}
}
}
before
test.csv(Source file)
"1","Battleship","Kongo","JPN"
"2","Heavy cruiser","Takao","JPN"
"3","Destroyer","Shigure","JPN"
after
test.csv(Source file)
00000001 Battleship, Kongo, Kongo, JPN, Battleship, Kongo-class battleship, Kongo-class battleship, Kongo-class battleship, Kongo-class battleship, Kongo-class battleship, JPN
00000002 Heavy cruiser, Takao, JPN
00000003 Destroyer, Shigure, JPN, Destroyer, Shigure, Shigure, JPN
I want to get used to using enums more and more.
Recommended Posts