1

I need to read a file and write into a separate file. I must also be able to read two files and write them into a single file.

File 1(To be read)
Header
text 
text
Footer

File 2(To be read)
Header
Text12
Text12
Footer

Output file

Header
text
text
Text12
Text12
Footer

the first line and last line remains same but only the middle lines are appended. According to my code. The header and footer is appended twice.

My output :
Header
text 
text
Footer
Header
Text12
Text12
Footer



My Code :

for (int i = 0; i < template.length; i++) {


        String endTime = findEndTime(startTime, duration);

        File file = new File(foldername);
        file.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(
                foldername + "/" + solfilename, true));
        BufferedWriter bsftp = new BufferedWriter(new FileWriter(
                "c:/ToolSOlFile/" + solfilename, true));


        try {
            String verify, putData = null,header=null,footer=null;

            FileReader fr = new FileReader("C:/ToolSOlFile/Templates/"+ template[i]);
            BufferedReader br = new BufferedReader(fr);

            while ((verify = br.readLine()) != null) { 


                    putData = verify.replace("YYYYMMDD", yyyymmdd);

                    putData = putData.replace("DD", duration);


                    putData = putData.replace("IIIIIIIIIIIIIII", imsi);


                    putData = putData.replace("HHMMSS", startTime);

                    putData = putData.replace("hhmmss", endTime);

                    putData = putData.replace("XXXXXXXXX", msisdn);

                    putData = putData.replace("BBBBBBBBBBBBBB",
                            processor.returnTemplateName(template[i]));


                    bw.append(putData + "\n");
                    bsftp.append(putData + "\n");

                }


            }

            bw.flush();
            bw.close();

            bsftp.flush();
            bsftp.close();
            br.close();

            startTime = findUpdatedStartTime(startTime);

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

    }
4
  • 2
    You haven't shown any code Commented Feb 10, 2015 at 5:43
  • read footer only from first file, store it in String and write at last. read header from first file only Commented Feb 10, 2015 at 5:46
  • @user7 code attached Commented Feb 10, 2015 at 5:54
  • you are not using header and footer variable. assign them value and use only once while writing. Commented Feb 10, 2015 at 6:54

1 Answer 1

1

Try the code given below it should help you to identify header footer and middle lines, then u can play around and write them into a file as u wish

try {

                reader = new BufferedReader(fr);
                String next, line = reader.readLine();
                for (boolean first = true, last = (line == null); !last; first = false, line = next) {
                    last = ((next = reader.readLine()) == null);


                    if (first) {

                        header=line;
                        System.out.println("First Line : "+header);


                    } else if (last) {

                        footer=line;
                        System.out.println("Middle Line : "+footer);



                    } else {

                        middle=line;
                        System.out.println("Footer Line : "+middle);



                    }

                    //WriteBUlkFileWithDifferenttemplates(header,middle,footer);

                }System.out.println("\n \n");
            } finally {
                if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
            }





        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.