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 9166455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:09:29+00:00 2026-06-17T15:09:29+00:00

I have just started programming and I am stuck while creating a basic File

  • 0

I have just started programming and I am stuck while creating a basic File I/O program in java.

The use case: I want to check for a string in a file and append a string in the same line. E.G. The file contents are as follows :

hostname=localhost
port=192

So, I want my program to look for hostname string in the above file and replace localhost with what ever value I pass to it.

I am able to get the file and pass the contents to a temporary file , but not sure how to manipulate strings in the file. Any help is highly appreciated.

  • 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-06-17T15:09:30+00:00Added an answer on June 17, 2026 at 3:09 pm

    Here are two ways (basic without any error/exception handling and passing target and replacement as arguments) how you can do it.

    If your file stores key/value pairs than the best way is to user java.util.Properties

    public class ReplaceInFile {
    
        private final static String src = "test.txt";
        private final static String dst_str = "test_new_str.txt";
        private final static String dst_prop = "test_new_prop.txt";
    
        public static void main(String[] args) throws IOException {
            usingStringOperations();
            usingProperties();
        }
    
        private static void usingProperties() throws IOException {
            File srcFile = new File(src);
            FileInputStream fis = new FileInputStream(srcFile);
            Properties properties = new Properties();
            properties.load(fis);
            fis.close();
            if(properties.getProperty("hostname") != null) {
                properties.setProperty("hostname", "127.0.0.1");
                FileOutputStream fos = new FileOutputStream(dst_prop);
                properties.store(fos, "Using java.util.Properties");
                fos.close();
            }
        }
    
        private static void usingStringOperations() throws IOException {
            File srcFile = new File(src);
            FileInputStream fis = new FileInputStream(srcFile);
            int len = fis.available();
            if(len > 0) {
                byte[] fileBytes = new byte[len];
                fis.read(fileBytes, 0, len);
                fis.close();
                String strContent = new String(fileBytes);
                int i = strContent.indexOf("localhost");
                if(i != -1) {
                    String newStrContent = strContent.substring(0, i) + 
                            "127.0.0.1" +
                            strContent.substring(i + "localhost".length(), strContent.length());
                    FileOutputStream fos = new FileOutputStream(dst_str);
                    fos.write(newStrContent.getBytes());
                    fos.close();    
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started with Gui Programming in netbeans (Using the template Java Desktop
I have just started to learn the very basics of Java programming. Using a
I have just started programming and have made a few small applications in C
I just started programming in Basic4android. I have created 2 layouts. splash and search
I have just started the Assembly language programming and in the first lecture our
I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
I have just started leaning and working on xquery with java. I have a
I have just started learning python version 3 and trying to create a file
I'm a real noob who just started learning 3d programming and i have a
i have just started programming for android and i have a little question i

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.