telug text file read , replace word , with other. java program

 




import java.io.*;

import java.text.SimpleDateFormat;

import java.util.*;


public class TeluguFileProcessor {

    public static void main(String[] args) {

        String inputFilePath = "C:\\Deviprasad\\input\\telugu_input.txt";

        String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        String outputFilePath = "C:\\Deviprasad\\output\\telugu_output_" + timestamp + ".txt";


        // Telugu word to replace

        String teluguWordToRemove = "తెలుగు";


        Set<String> uniqueLines = new LinkedHashSet<>();

        Map<String, List<Integer>> duplicates = new LinkedHashMap<>();


        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilePath), "UTF-8"));

             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8"))) {


            String line;

            int lineNumber = 1;


            while ((line = reader.readLine()) != null) {

                // Replace Telugu word

                String updatedLine = line.replaceAll(teluguWordToRemove, "").trim();


                if (!updatedLine.isEmpty()) {

                    if (!uniqueLines.contains(updatedLine)) {

                        uniqueLines.add(updatedLine);

                    } else {

                        duplicates.computeIfAbsent(updatedLine, k -> new ArrayList<>()).add(lineNumber);

                    }

                }


                lineNumber++;

            }


            // Write unique lines

            for (String l : uniqueLines) {

                writer.write(l);

                writer.newLine();

            }


            // Print duplicates

            if (!duplicates.isEmpty()) {

                System.out.println("Duplicate Telugu lines found:");

                for (Map.Entry<String, List<Integer>> entry : duplicates.entrySet()) {

                    System.out.println("Line: \"" + entry.getKey() + "\" repeated at rows " + entry.getValue());

                }

            } else {

                System.out.println("No duplicate Telugu lines found.");

            }


            System.out.println("Output file created: " + outputFilePath);


        } catch (IOException e) {

            System.err.println("Error: " + e.getMessage());

        }

    }

}

/*

నేను తెలుగు నేర్చుకుంటున్నాను
తెలుగు ఒక అందమైన భాష
తెలుగు ఒక అందమైన భాష
నేను తెలుగు నేర్చుకుంటున్నాను
మన దేశ భాషలందు తెలుగు లెస్స


నేను  నేర్చుకుంటున్నాను
ఒక అందమైన భాష
మన దేశ భాషలందు  లెస్స
...............
Duplicate Telugu lines found:
Line: "ఒక అందమైన భాష" repeated at rows [3]
Line: "నేను  నేర్చుకుంటున్నాను" repeated at rows [4]
Output file created: C:\Deviprasad\output\telugu_output_20250409_195255.txt
*/