2

I'm trying to introduce a line break at every 100th character of the line from the existing file.But it doesn't write anything to it. below is the code written in java to read the existing file and write to it with a temporary file.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReplaceFileContents {
   public static void main(String[] args) {
     new ReplaceFileContents().replace();
   }

   public void replace() {
      String oldFileName = "Changed1.ldif";
      String tmpFileName = "Changed2.ldif";

      BufferedReader br = null;
      BufferedWriter bw = null;
      try {
         br = new BufferedReader(new FileReader(oldFileName));
         bw = new BufferedWriter(new FileWriter(tmpFileName));
         String line;
         while ((line = br.readLine()) != null) {       
                line.replaceAll("(.{100})", "$1\n");       
         }
      } catch (Exception e) {
         return;
      } finally {
         try {
            if(br != null)
               br.close();
         } catch (IOException e) {
            //
         }
         try {
            if(bw != null)
               bw.close();
         } catch (IOException e) {
            //
         }
      }
      // Once everything is complete, delete old file..
      File oldFile = new File(oldFileName);
      oldFile.delete();

      // And rename tmp file's name to old file name
      File newFile = new File(tmpFileName);
      newFile.renameTo(oldFile);

   }
}
4
  • 1
    Do not silence the Exception !! try out output them on the console. Commented Apr 27, 2016 at 12:26
  • 5
    You are not writing to bw anywhere Commented Apr 27, 2016 at 12:27
  • instead of reading and processing the file line by line, why not just read one byte at a time whilst keeping track of the bytes consumed and once the count reaches 100 append a new line. Commented Apr 27, 2016 at 12:28
  • Final note: read about try-with-resources. With recent versions of java, you don't need all that manual closing of writers/readers/... Commented Apr 27, 2016 at 12:32

2 Answers 2

7
while ((line = br.readLine()) != null) {       
    line.replaceAll("(.{100})", "$1\n");       
}

First off, line.replaceAll does not replace your line variable with the result. Because Strings are immutable, this method returns the new string, so your line should be line = line.replaceAll(....

Second, you're never writing the new String back into the file. Using replaceAll doesn't change the file itself in any way. Instead, try using your bw object to write the new String to the same line.

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

2 Comments

Thanks It writes.But what I really expected is to have a line break say after 4th character. Input ----- abcdef gh ijklmn output ------ abcd ef gh ijkl mn but the above codes writes all in a single line.
BufferedReader.read will read one character at a time. BufferedWriter.write will write without making a new line afterward.
2

From what you've published here, you never try to write line back to bw. Try this:

package hello;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        new Test().replace();
    }

    public void replace() {
        String oldFileName = "d:\\1.txt";
        String tmpFileName = "d:\\2.txt";

        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(oldFileName));
            bw = new BufferedWriter(new FileWriter(tmpFileName));
            String line;
            while ((line = br.readLine()) != null) {
                line = line.replaceAll("(.{100})", "$1\n");
                bw.write(line);
            }
        } catch (Exception e) {
            return;
        } finally {
            try {
                if(br != null)
                    br.close();
            } catch (IOException e) {
                //
            }
            try {
                if(bw != null)
                    bw.close();
            } catch (IOException e) {
                //
            }
        }
        // Once everything is complete, delete old file..
        File oldFile = new File(oldFileName);
        oldFile.delete();

        // And rename tmp file's name to old file name
        File newFile = new File(tmpFileName);
        newFile.renameTo(oldFile);

    }
}
  1. You never try to write line back to bw;
  2. String#replaceAll will return the copy of the source not the original String;

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.