I am trying to create a text file and in that text file i have different columns of different size and i have to write accordingly in that file,
I have tried like that …..
but I dont know how to define the size for each column for example i want the location for size 30 not less not more how can i do that ?
BufferedWriter bw = null;
try {
String none=" ";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
int no_of_files=01;
String line1 = "H"+StringUtils.rightPad(none,9)+dateFormat.format(date)+StringUtils.rightPad(none,8)+" "+no_of_files+StringUtils.rightPad(none,10)+StringUtils.rightPad(none,290);
String line2 =StringUtils.rightPad("CID",10)+StringUtils.rightPad("Location",30)+StringUtils.rightPad("DateSF",8)+StringUtils.rightPad("DateST",8)+StringUtils.rightPad("BillAmt",10)+StringUtils.rightPad("BR",2)+StringUtils.rightPad("PDF FileName",260);
File file = new File("D:\\write.txt");
bw = new BufferedWriter(new FileWriter(file));
bw.write(line1);
bw.newLine();
bw.write(line2);
// writer.write(line2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I would look into using this function from Commons-IO to help keep the length of each column the same.
StringUtils.rightPad()