I am having trouble splitting the records of a marc21 format file. I am reading from one file and trying to separate the records into separate lines, then write into a different file. Here is what I currently have:
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try{
fr = new FileReader("data.txt");
br = new BufferedReader(fr);
fw = new FileWriter("SplitRecords.txt");
bw = new BufferedWriter(fw);
String data;
String recordLength = "";
int intLength = 0;
int lengthStart = 0;
int lengthEnd = 5;
while((data = br.readLine()) != null){
while(data != null){
recordLength = data.substring(lengthStart, lengthEnd);
System.out.println(recordLength);
intLength = Integer.parseInt(recordLength);
bw.write(data, lengthStart, intLength);
bw.write("\n");
bw.flush();
lengthStart = intLength;
lengthEnd = lengthStart + 5;
br.mark(intLength);
br.reset();
}
}
}
finally{
if(fr != null){
fr.close();
}
if(br != null){
br.close();
}
if(fw != null){
fw.close();
}
if(bw != null){
bw.close();
}
}
}
}
This is the output and error I am getting:
00934
00699
1cRT
Exception in thread "main" java.lang.NumberFormatException: For input string: "1cRT"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Main.main(Main.java:26)
It writes for the first record and the second into the file, however the third loop does not read the length properly. Does anyone have any idea why is this happening?