public static void main(String[] args)
{
System.out.println("--- Start splitting --- " + (new Date()).toString());
SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
int errors = 0;
String rootDirectory = "d:/zzz/";
try
{
// Get partin filename
String entries1[] = new File(rootDirectory + "partin/").list();
String partinFilename = rootDirectory + "partin/" + entries1[0];
// Get pricat filename
String entries2[] = new File(rootDirectory + "pricat/").list();
String pricatFilename = rootDirectory + "pricat/" + entries2[0];
// Variables
String outputFolder = rootDirectory + "output/";
File partinInputFile = new File(partinFilename);
File pricatInputFile = new File(pricatFilename);
FileReader partinInputStream = new FileReader(partinInputFile);
BufferedReader partin = new BufferedReader(partinInputStream);
BufferedWriter pricatOutput = null;
String partinRow = "";
String pricatRow = "";
String header = "";
String partinBpCd = "";
String partinOdsCd = "";
String partinCustNb = "";
String pricatBpCd = "";
String pricatOdsCd = "";
String pricatCustNb = "";
boolean headerSet = false;
// Process pricat file for every partin entry
while ((partinRow = partin.readLine()) != null)
{
if (!partinRow.substring(0, 5).trim().equals("."))
{
partinBpCd = partinRow.substring(0, 5).trim();
partinOdsCd = partinRow.substring(5, 10).trim();
partinCustNb = partinRow.substring(60, 75).trim();
System.out.println("---");
System.out.println("BpCd: " + partinBpCd);
System.out.println("OdsCd: " + partinOdsCd);
System.out.println("CustNb: " + partinCustNb);
System.out.println("---");
FileReader pricatInputStream = new FileReader(pricatInputFile);
BufferedReader pricat = new BufferedReader(pricatInputStream);
int rowcount = 0;
while ((pricatRow = pricat.readLine()) != null)
{
// check rowtype
if (pricatRow.substring(40, 46).equals("LINHDR"))
{
header = pricatRow;
headerSet = false;
}
else if (pricatRow.substring(40, 46).equals("LINMDC"))
{
pricatBpCd = pricatRow.substring(0, 5).trim();
pricatOdsCd = pricatRow.substring(5, 10).trim();
pricatCustNb = pricatRow.substring(95, 110).trim();
// Check if partin & pricat data is equal
if ((pricatBpCd.equals(partinBpCd))
&& (pricatOdsCd.equals(partinOdsCd))
&& (pricatCustNb.equals(partinCustNb)))
{
String outputFilename = outputFolder
+ pricatBpCd
+ pricatOdsCd
+ pricatCustNb
+ df.format(new Date());
// Delete old output file and create a new one
if (rowcount == 0)
{
if (new File(outputFilename).exists())
new File(outputFilename).delete();
File outputFile = new File(outputFilename);
pricatOutput = new BufferedWriter(new FileWriter(outputFile));
}
// Write header
if (!headerSet)
{
pricatOutput.write(header + "\n");
headerSet = true;
}
// Write line
pricatOutput.write(pricatRow + "\n");
rowcount++;
}
}
else if (!pricatRow.substring(40, 46).equals("MSGTRL"))
{
errors++;
}
}
// Close pricat input stream
pricat.close();
}
}
// Close partin input stream
partin.close();
// Close pricat output stream
pricatOutput.close();
}
catch (IOException e)
{
System.out.println(e);
}
System.out.println("--- End splitting --- " + (new Date()).toString());
System.out.println("Errors: " + errors);
}