Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3978294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:01:48+00:00 2026-05-20T05:01:48+00:00

What is the fastest way to do a search and replace on a string

  • 0

What is the fastest way to do a search and replace on a string in an existing file using Java?

Let’s say I have this:

// This references an existing file 
File file = ...

The file in question looks like this:

The Green Dog is furry.
It likes to run in the Green Grass.
Green is its favorite color.

How would I go about replacing the String “Green” with “Blue” and having that file re-saved with the new color?

Update:
I’ve thought about this a little more and perhaps the best and fastest way is to just read the contents of the file into a string (using something like FileUtils) and then just doing a replace and re-writing to the file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:01:48+00:00Added an answer on May 20, 2026 at 5:01 am

    Have a look at Retrieving and Replacing number in text file which is pretty much the same.

    Edit: Regarding your update, I would just use BufferedReader and BufferedWriter and leave it to the JVM to optimise reads/writes, i.e. I would do the replacements in a streaming fashion. Your suggested solution of reading to memory could be a bit faster – but I wouldn’t read everything in memory (makes approach not scalable) unless there’s a very good reason.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering what is the fastest, most efficient way (using JAVA) to search
What's the fastest way using either DOS scripting or PowerShell to run this simple
What would be the fastest way to search for a file programtically in C#.
What is the fastest way to get lists of files and search through file
I'm using Eclipse PDT for PHP . Let's say I have an error in
Let's say we have a big read only list of (int, string) elements. What
What is the fastest way to share data structures between Java and C#? I
Consider i have an excel file >200000 rows in it. What is the fastest
I have this very huge XML file of size 2.8GB. This is Polish Wikipedia's
what's the probably fastest way of reading relatively huge files with Java's I/O-methods? My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.