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();
}
}
}
}