3

How can I replace text in a file that is too large for a String? I have been using the following to replace text in most of the files I've seen:

File f = new File("test.xml");
String content = FileUtils.readFileToString(f, "UTF-8");

content = content.replaceFirst("some text", "new text");
FileUtils.writeStringToFile(f, content, "UTF-8");

This works well for files that are normal sized. However, some of the files I have been getting are very large (too large to store in a String) and they cause an overflow. How can I replace text in those files?

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
  • How big is the file ?? – Biswajit Oct 25 '16 at 13:46
  • 4
    You could try and use a buffered `Reader`, `Writer` and replace within each line while writing back to another file. – Mena Oct 25 '16 at 13:46
  • BufferedReader and BufferedWriter are the way to read/write file in Java in most of the case. – Cédric O. Oct 25 '16 at 13:50
  • This answer details how to read files more efficiently: http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file?rq=1 – reden Oct 25 '16 at 13:57
  • You might be able to use a random access file, https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html – patrik Oct 25 '16 at 14:21

4 Answers4

3

You can use sed command to replace string of large file like below [Linux]

String[] cmdarr = {"bash", "-c", "sed 's/oldstring/newstring/g' input.txt > output.txt"};
Process runCmd = Runtime.getRuntime().exec(cmdarr); 

in Windows you can install sed, use some thing like

String[] cmdarr = {"cmd", "/c", "sed 's/oldstring/newstring/g' input.txt > output.txt"};
Process runCmd = Runtime.getRuntime().exec(cmdarr);

more on Link

Md Ayub Ali Sarker
  • 10,795
  • 4
  • 24
  • 19
1

Try using a FileInputStream to read it line-wise

FileInputStream fs = new FileInputStream("test.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fs));

etc.

Sorry if there are any errors in the code, i can't try the code myself right now

DLIK
  • 50
  • 7
1
try {
            File f=new File("test.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            String content=null;

            while((content=reader.readLine())!=null)
            {
                content = content.replaceFirst("some text", "new text");
                System.out.println(content);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Keval Pithva
  • 600
  • 2
  • 5
  • 21
-1

Try to use FART(find and replace text) for windows: fart -c big_filename.txt "find_this_text" "replace_to_this"

Sergey Frolov
  • 1,317
  • 1
  • 16
  • 30