enum Ich habe es nicht so oft benutzt, also habe ich es in der Praxis gemacht. Praktikabilität ist unbekannt. Die Java-Version ist 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 {
//Datensatzdefinition: Anzahl der Zeichen
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;
}
}
//Datensatzdefinition:(Standardzeichen,Polsterzeichen)
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;
}
}
/**
*Hauptklasse
*/
public static void main(String[] args) throws IOException {
SamleFixedLengthFromCsv proc = new SamleFixedLengthFromCsv();
proc.execute();
}
/**
*Lauf
*/
protected void execute() {
try {
mainProc();
} catch (Exception e) {
System.out.println("Dateiausgabe fehlgeschlagen ...");
}
}
/**
*Hauptprozess
*/
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();
}
}
}
/**
*Erstellung von Datensätzen mit fester Länge
*/
private String createFixedRecords() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < recordVal.values().length; i++) {
sb.append(createFixedColumn(i));
}
return sb.toString();
}
/**
*Elemente in feste Länge konvertieren
*/
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);
}
}
//Zahlen sind rechtsbündig, sonst linksbündig
try {
Integer.parseInt(val.toString());
return tempPad.append(val).toString();
} catch(Exception e) {
return val.append(tempPad.toString()).toString();
}
}
}
before
test.csv(Konvertierungsquelldatei)
"1","Schlachtschiff","Kongo","JPN"
"2","Schwerer Kreuzer","Takao","JPN"
"3","Zerstörer","Shigure","JPN"
after
test.csv(Konvertierungsquelldatei)
00000001 Schlachtschiff, Kongo, JPN, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥
00000002 Schwerer Kreuzer, Kaohsiung, JPN, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥
00000003 Zerstörer, Shigure, JPN, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥, ‥
Ich möchte mich immer mehr daran gewöhnen, Enum zu verwenden.
Recommended Posts