java and groovy : read file from source locatoin and place into another locatoin , using arraylist read and display

 


java

----

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) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

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

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

        String outputPath = "C:\\Users\\04758W744\\Desktop\\456.txt"; // Replace with your desired output path


        try {

            List<String> lines = readFile(filePath);


            // Filter lines starting with "a" and append a timestamp

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

            

           


            for (String line : lines) {

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

                    filteredLines.add(line);

                }

            }


            // Display filtered lines and append them to the output file

            for (String filteredLine : filteredLines) {

                System.out.println(filteredLine);

            }


            appendLinesToFile(outputPath, 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 void appendLinesToFile(String outputPath, List<String> lines) throws IOException {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath, true))) {

            for (String line : lines) {

                writer.write(line);

                writer.newLine();

            }

        }

    }

}


--------------------------

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

try {
    def lines = readFile(filePath)

    // Filter lines starting with "a" and append a timestamp
    def filteredLines = []
    def timestamp = new Date().format("yyyy-MM-dd HH:mm:ss")

    lines.each { line ->
        if (line.startsWith("a")) {
            filteredLines << " ${line}"
        }
    }

    // Display filtered lines and append them to the output file
    filteredLines.each { filteredLine ->
        println filteredLine
    }

    appendLinesToFile(outputPath, filteredLines)
} catch (IOException e) {
    e.printStackTrace()
}

def appendLinesToFile(String outputPath, List<String> lines) {
    new File(outputPath).withWriterAppend { 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