I am trying to Format file, but I am getting error
File Look like this:
H10288720130719000000600000000245000000355600156646E
200000001 001538 M000 00000 1 0000 A5008390102730000153800717230080 901027300
200000002 001545 M000 00000 1 0000 A5008390102730000154500730383135 901027300
200000356 576270 201302B 250A CHILDS WORLD LEARNING CENTER S 1725 MCCALLIE AVE CHATTANOOGA TN37404S 1200 N HOLTZCLAW AVE CHATTANOOGA TN37406-30190071200 N HOLTZCLAW AVE 1 0000 A0008390102730057627037404302425 901027300
200000357 635571 201306FW250AHLEMANN JAMES P 87 PO BOX HIGH VIEW WV26808 TEMPORARILY AWAY TEMPORARILY AWAY 1 0000 A0008390102730063557126808008787 901027300
if (32,33) is 2 than write in file,
Here is my code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class usps1 {
/**
* @param args
*/
public static void delFileFromDir(String dirPath) {
File dir = new File(dirPath);
if (dir.listFiles() == null)
return;
for (File file : dir.listFiles()) {
if (!file.isDirectory())
file.delete();
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File folderout = new File("FileOut");
File[] listOfFilesout = folderout.listFiles();
String filesout;
for (int iout = 0; iout < listOfFilesout.length; iout++) {
if (listOfFilesout[iout].isFile()) {
filesout = listOfFilesout[iout].getName();
if(listOfFilesout != null) {
try {
File movefilesfromout = new File("FileOut\\" + filesout);
//File movefilesto = new File("archive\\letters\\" + movefilesfrom.getName());
if (movefilesfromout.renameTo(new File("FileOut\\Done\\" + movefilesfromout.getName()))) {
System.out.println("Moves files ======================================================================>" + movefilesfromout);
}
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
}
BufferedReader br = null;
BufferedWriter bfAll = null;
String delimiter = "|";
File folder = new File("FileIn");
System.out.println(folder);
File[] BFFile = folder.listFiles();
System.out.println(BFFile);
for (File file : BFFile) {
br = new BufferedReader(new FileReader(file));
String filename = file.getName();
String[] fileInitialName = filename.split("\\.");
String outFileAll = ("FileOut" + File.separator
+ fileInitialName[0] + "_All.txt");
File oldFileAll = new File("FileOutAll"
+ File.separator + file.getName());
oldFileAll.delete();
bfAll = new BufferedWriter(new FileWriter(outFileAll));
String line;
line = br.readLine();
System.out.println(line);
int invoiceLine = 0;
StringBuilder tempILlines = new StringBuilder();
int aCountAll = 0;
StringBuffer headerAll = new StringBuffer("Record Type ID" + delimiter +
"Sequence Number" + delimiter +
"Participant Code" + delimiter +
"Keyline" + delimiter +
"Move Effective Date" + delimiter +
"Move Type" + delimiter +
"Deliverability Code" + delimiter +
"USPS Site ID" + delimiter +
"COA Name" + delimiter +
"Old Address Type" + delimiter +
"Old Urbanization Name" + delimiter +
"Parsed Old Address" + delimiter +
"Old City" + delimiter +
"Old State" + delimiter +
"Old Zip" + delimiter +
"New Address Type" + delimiter +
"New Urbanization Name" + delimiter +
"Parsed New Address" + delimiter +
"New City" + delimiter +
"New State" + delimiter +
"New Zip" + delimiter +
"Label Format New Address" + delimiter +
"Filler" + delimiter +
"Postage Due" + delimiter +
"PMB Info" + delimiter +
"Class/Notification Type" + delimiter);
bfAll.write(headerAll.toString());
bfAll.newLine();
String writeFile = "";
int flagAll = 0;
int recordcountOfAll = 0;
while ((line = br.readLine()) != null) {
String typechk = line.substring(32,33);
if ((typechk.equals("2"))) {
writeFile = "All";
}
if (writeFile.equals("All")) {
if ((aCountAll == 1)) {
bfAll.write(invoiceLine + delimiter);
bfAll.write(tempILlines.toString());
bfAll.newLine();
aCountAll = 0;
invoiceLine = 0;
tempILlines.delete(0, tempILlines.length());
}
flagAll = 1;
aCountAll = 1;
bfAll.write(line.substring(32, 37)
+ delimiter);
invoiceLine++;
}
bfAll.write(invoiceLine + delimiter);
bfAll.write(tempILlines.toString());
bfAll.newLine();
bfAll.flush();
bfAll.close();
if (flagAll == 0) {
File fl = new File(outFileAll);
fl.delete();
} else {
File oldOutAll = new File(outFileAll);
File newOutAll = new File("FileOut"
+ File.separator + fileInitialName[0]
+ "_All-recordcount-" + recordcountOfAll + ".txt");
oldOutAll.renameTo(newOutAll);
oldOutAll.delete();
}
}
}
}
}
I am getting error:
FileIn
[Ljava.io.File;@7ffe01
H10288720130719000000600000000245000000355600156646E
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 33
at java.lang.String.substring(Unknown Source)
at usps1.main(usps1.java:135)
I don't know, why I am getting error, Please help me!! Thanks!!
This is line 135:
String typechk = line.substring(32,33);