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

  • SEARCH
  • Home
  • 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 3336836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:10:59+00:00 2026-05-18T00:10:59+00:00

How can I read an existing text file and modify some of its value

  • 0

How can I read an existing text file and modify some of its value based on some condition like if I get some text like

Apple 1 3 5

Now, here whenever I get an integer value I have to increase its count by 1 and save back to the same file again.

For example, if I read some content like above I should be able to save

Apple 2 4 6

back to the same file again

My code looks like

try{
FileWriter fstream = new FileWriter(“c:\Apple.txt”,true);

        FileReader inputFileReader   = new FileReader("c:\\Apple.txt");
        BufferedReader inputStream   = new BufferedReader(inputFileReader);
        PrintWriter    outputStream  = new PrintWriter(fstream);



        while((inline=inputStream.readLine())!=null)
        {
            values=inline.split("-");

            Integer l = Integer.parseInt(values[2])+1;
            outputStream.print((inline.replaceAll(values[2],l.toString())));

        }

        outputStream.close();


    }

So, I get an output like

Apple 1 3 5 Apple 1 4 5

But my required output is

Apple 1 4 5

Thanks in advace

  • 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-18T00:10:59+00:00Added an answer on May 18, 2026 at 12:10 am

    You can’t do it interactively on the same file (or at least maybe you can but it would be really tricky).. you could

    • open the input file
    • open the output file
    • for each line in file
      • check your condition, if true
        • generate the new line according to what you are going to modify
        • save it on output file
      • if false
        • copy the line into new file without modifying it
    • close both files
    • delete first one and rename second one to first (you can do it easily with API functions)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How you can read a file (text or binary) from a batch file? There
I can read settings like this, for example: final String mytest = System.getString(this.getContentResolver(), System.AIRPLANE_MODE_ON);
I can read XLS file with this code : string path =@c:\r\1.xlsx; OleDbConnection MyConnection
In .Net you can read a string value into another data type using either
We are having to read text off of an existing VB6 application. So we
How can I make the code below to read correct text. In my text
I've been storing some simple user click-actions in text files so I can easily
I have an existing PDF and I can use FdFWriter to input to text
I can read the current delivery count using javax.jms.Message.getIntProperty(JMSXDeliveryCount). I'm trying to read the
Anyone can read the GoF book to learn what design patterns are and how

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.