read file , assign to arraylist and read with condition, for any flat file in java

 abc,123,1235

1,2,3

2,3,4

abc,123,1235

4,5,6

6,7,8

9,0,0



import java.io.*;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;


public class FileFilterAndAppend {

    public static void main(String[] args) {

        String filePath = "C:\\Users\\04758W744\\Desktop\\123.txt"; // Replace with your file path


        try {

            List<String> lines = readFile(filePath);

            List<String> filteredLines = filterLinesStartingWithA(lines);


            // Display filtered lines

            for (String line : filteredLines) {

                System.out.println(line);

            }


            // Save filtered lines to a new file with a timestamp

            saveFilteredLinesToFile(filePath, filteredLines);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    static List<String> readFile(String filePath) throws IOException {

        List<String> lines = new ArrayList<>();


        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {

            String line;

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

                lines.add(line);

            }

        }


        return lines;

    }


    static List<String> filterLinesStartingWithA(List<String> lines) {

        List<String> filteredLines = new ArrayList<>();

        for (String line : lines) {

            if (line.startsWith("a") || line.startsWith("A")) {

                filteredLines.add(line);

            }

        }

        return filteredLines;

    }


    static void saveFilteredLinesToFile(String filePath, List<String> lines) throws IOException {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

        String timestamp = dateFormat.format(new Date());

        String outputFilePath = filePath.replace(".txt", "_" + timestamp + ".txt");


        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            for (String line : lines) {

                writer.write(line);

                writer.newLine();

            }

        }

    }

}

--------------------
def filePath = "C:\\path\\to\\your\\file.txt" // Replace with your file path

try {
    def lines = readFile(filePath)
    def filteredLines = filterLinesStartingWithA(lines)

    // Display filtered lines
    filteredLines.each { line ->
        println line
    }

    // Save filtered lines to a new file with a timestamp
    saveFilteredLinesToFile(filePath, filteredLines)
} catch (IOException e) {
    e.printStackTrace()
}

List<String> readFile(String filePath) throws IOException {
    def lines = []

    new File(filePath).withReader { reader ->
        reader.eachLine { line ->
            lines << line
        }
    }

    return lines
}

List<String> filterLinesStartingWithA(List<String> lines) {
    def filteredLines = []
    lines.each { line ->
        if (line.startsWith("a") || line.startsWith("A")) {
            filteredLines << line
        }
    }
    return filteredLines
}

void saveFilteredLinesToFile(String filePath, List<String> lines) throws IOException {
    def dateFormat = new SimpleDateFormat("yyyyMMddHHmmss")
    def timestamp = dateFormat.format(new Date())
    def outputFilePath = filePath.replaceAll("\\.txt$", "_${timestamp}.txt")

    new File(outputFilePath).withWriter { writer ->
        lines.each { line ->
            writer.writeLine(line)
        }
    }
}
------------------------

Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024