user3678383
user3678383

Reputation: 105

How to append a line in a file or before encountering newline character?

Input File:

Online_system_id
bank_details
payee
credit_limit
loan_amount

Online_system_id
bank_details
payee
credit_limit
loan_amount

Expected Output:

Online_syatem_id
bank_details
payee
credit_limit
loan_amount
proc_online_system_id

Online_syatem_id
bank_details
payee
credit_limit
loan_amount
proc_online_system_id

Below is the code given for reference.
I want to add a line after each record i.e before encountering the blank line.
What changes do I need to do?

String line;
int flag=0;
PrintStream out = new PrintStream(outputFile);
BufferedReader br = new BufferedReader(new FileReader(outputFile));
while((line=br.readLine())!=null){
    if(!line.contains("proc_online_system_id")){
        flag=1;                 
    }

}
if(flag==1)
    out.print("proc_online_system_id");

Upvotes: 4

Views: 105

Answers (3)

vefthym
vefthym

Reputation: 7462

String line;
PrintStream out = null;
BufferedReader br = null;
try {
  out = new PrintStream(new FileOutputStream(outputFile)); 
  br = new BufferedReader(new FileReader(inputFile)); 
  while((line=br.readLine())!=null){
    if(line.trim().isEmpty()) {            
        out.println("proc_online_system_id");   //print what you want here, BEFORE printing the current line         
    }
    out.println(line);        //always print the current line
  }

} catch (IOException e) {
    System.err.println(e);
} finally { 
  try{
    out.close(); 
    br.close();
  } catch (Exception ex) {
    System.err.println(ex);
  }
}

And don't forget the out.close(); and br.close(); afterwards. This solution stores only the current line in memory, as opposed to Dakkaron's answer, which is correct, but needs to store the whole file in memory (in a StringBuilder instance), before writing to file.

EDIT: After Vixen's comment, here is the link, in case you have java 7, and you want to use try with resources in your solution.

Upvotes: 1

Dakkaron
Dakkaron

Reputation: 6278

Buffer each block. So what you do is read the file line by line and store the content of the current block in a StringBuilder. When you encounter the empty line, append your additional data. When you did that with the whole file, write the content of the StringBuilder to the file.

String line;
int flag=0;
PrintStream out = new PrintStream(outputFile);
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(outputFile));
while((line=br.readLine())!=null){
    if(!line.contains("proc_online_system_id")){
         flag=1;                
    }
    if (line.isEmpty() && flag==1) {
        flag=0;
        builder.append("proc_online_system_id\n");
    }
    builder.append(line).append("\n");
}
out.print(builder.toString());

Upvotes: 1

aksappy
aksappy

Reputation: 3400

Try this code

String line;
PrintStream out = new PrintStream(outputFile);
BufferedReader br = new BufferedReader(new FileReader(outputFile));
while((line=br.readLine())!=null){
   if (!line.trim().isEmpty()){
      line+="\n";
    }
   //System.out.println(line);       
} 

Upvotes: 1

Related Questions