Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Java Program that replaces a line from a file containing X with Y

Java Program that replaces a line from a file containing “new H1”

Posted on December 18, 2023 by Toma Velev

Below is a simple Java program that reads a file, searches for a line containing “new H1”, and replaces it with a new line.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class ReplaceLineInFile { public static void main(String[] args) { String filePath = "path/to/your/file.txt"; // Replace with your file path String searchString = "new H1"; String replacementString = "new H1 Replacement"; try { replaceLine(filePath, searchString, replacementString); System.out.println("Line replaced successfully!"); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } } public static void replaceLine(String filePath, String searchString, String replacementString) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(filePath)); StringBuilder content = new StringBuilder(); // Read the file content and replace the line String line; while ((line = reader.readLine()) != null) { if (line.contains(searchString)) { content.append(replacementString).append(System.lineSeparator()); } else { content.append(line).append(System.lineSeparator()); } } reader.close(); // Write the modified content back to the file BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); writer.write(content.toString()); writer.close(); } }

Replace the filePath variable with the path to your actual file. The program reads the file, searches for a line containing “new H1”, replaces it with the specified replacementString, and writes the modified content back to the file.

This program is generated by ChatGPT. Read more articles on the topic of AI on my blog: https://programtom.com/dev/category/software-development/chatgpt/

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)