0

I've a code which replaces 10:A to 12:A in a text file called sample.txt. Also, the code I've now is changing the file format, which shouldn't. Can someone please let me know how to do the same using regular expression in Java which doesn't change the file format? File has original format as below
10:A
14:Saxws
But after executing the code it outputs as 10:A 14:Saxws.

import java.io.*;     
import java.util.*;     


public class FileReplace
{
    List<String> lines = new ArrayList<String>();
    String line = null;
    public void  doIt()
    {
        try
        {
            File f1 = new File("sample.txt");
            FileReader fr = new FileReader(f1);
            BufferedReader br = new BufferedReader(fr);
            while ((line = br.readLine()) != null)
            {
                if (line.contains("10:A"))
                    line = line.replaceAll("10:A", "12:A") + System.lineSeparator();
                lines.add(line);
            }
            fr.close();
            br.close();

            FileWriter fw = new FileWriter(f1);
            BufferedWriter out = new BufferedWriter(fw);
            for(String s : lines)
                 out.write(s);
            out.flush();
            out.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        FileReplace fr = new FileReplace();
        fr.doIt();
    }
}
5
  • "Also, the code I've now is changing the file format, which shouldn't" can you explain it in more details? How is your format being changed? Commented Apr 14, 2015 at 18:33
  • Added details in my question Commented Apr 14, 2015 at 18:36
  • Replace System.lineSeparator() with "\r\n". Commented Apr 14, 2015 at 18:40
  • It doesn't make much sense. It looks like your code removes line separators but line.replaceAll("10:A", "12:A") + System.lineSeparator(); adds them. Unless you have problem with reading them because separator added by System.lineSeparator() may be different than one used by your OS/editor. Commented Apr 14, 2015 at 18:42
  • Reading the whole file into memory will cause out of memory problems Commented Apr 14, 2015 at 19:34

1 Answer 1

2

It looks like your OS or editor is not able to print correctly line separators generated by System.lineSeparator(). In that case consider

  • reading content of entire file to string (including original line separators), - then replacing part which you are interested in
  • and writing replaced string back to your file

You can do it using this code:

Path file = Paths.get("sample.txt");

//read all bytes from file (they will include bytes representing used line separtors)
byte[] bytesFromFile = Files.readAllBytes(file);

//convert themm to string
String textFromFile = new String(bytesFromFile, StandardCharsets.UTF_8);//use proper charset

//replace what you need (line separators will stay the same)
textFromFile = textFromFile.replaceAll("10:A", "12:A");

//write back data to file
Files.write(file, textFromFile.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
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.