atzu
atzu

Reputation: 424

Read a single file with multiple BufferedReaders

I'm working on a program that needs to update a line that depends its value on the result of a line that goes read after. I thought that I could use two BufferedReaders in Java to position the reader on the line to update while the other one goes for the line that fixes the value (it can be an unknown number of lines ahead). The problem here is that I'm using two BufferedReaders on the same file and even if I think I'm doing right with the indexes the result in debug doesn't seem to be reliable. Here's the code:

            String outFinal
            FileName=fileOut;

        File fileDest=new File(outFinalFileName);
        try {
            fout = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(fileDest)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        FileReader inputFile=null;

        try {
            inputFile = new FileReader(inFileName);
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
        }
        BufferedReader fin = new BufferedReader(inputFile);

        BufferedReader finChecker = new BufferedReader(inputFile);  //Checks the file and matches record to change
        String line="";
        String lineC="";
        int lineNumber=0;
        String recordType="";
        String statusCode="";
        try {
            while ((lineC = finChecker.readLine()) != null) {
                lineNumber++;
                if (lineNumber==1)
                    line=fin.readLine();
                recordType=lineC.substring(0,3);//Gets current Record Type
                if (recordType.equals("35")){
                    while(!line.equals(lineC)){
                        line=fin.readLine();
                        if (line==null)
                            break;
                        fout.write(line);


                    }
                }else if (recordType.equals("32")){
                    statusCode=lineC.substring(4,7);
                    if(statusCode.equals("XX")){
                        updateRecordLine(line,fout);
                    }
                }


            }
            returnVal=true;

        } catch (IOException e) {
            e.printStackTrace();

        }

Thanks in advance.

Upvotes: 1

Views: 2108

Answers (2)

Jayy
Jayy

Reputation: 2436

ok, i see some problem in your code exactly on these lines-->

recordType=lineC.substring(0,3);//Gets current Record Type                 
if (recordType.equals("35")){ 

if you see on the first line, you are getting the substring of recordType into recordType. Now recordType length is 3. If at all the recordType has only 2 characters, then substring throws arrayIndexOutOfBoundsException. So when no runtime exceptions, its length is 3 and on the next line you are calling the equals method that has a string with 2 characters.

Will this if block ever run ?

Upvotes: 0

jefflunt
jefflunt

Reputation: 33954

Well, the BufferedReader only reads stuff, it doesn't have the ability to write data back out. So, what you would need is a BufferedReader to get stuff in, and a BufferedWriter that takes all the input from the BufferedReader, and outputs it to a temp file, with the corrected/appended data.

Then, when you're done (i.e. both BufferedReader and BufferedWriter streams are closed), you need to either discard the original file, or rename the temp file to the name of the original file.

You are basically copying the original file to a temp file, modifying the line in question in the temp file's output, and then copying/renaming the temp file over the original.

Upvotes: 2

Related Questions