Fileprocess - gsnaidujava/pp GitHub Wiki
package com.firstdata;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;
public class FileProcess {
private final static String NEWLINE = System.getProperty("line.separator");
public void removeDuplicateHeader(String inputFile, String outputPath)
throws IOException {
Scanner scanner = new Scanner(new File(inputFile)); BufferedReader br = new BufferedReader(new FileReader(inputFile)); String header = scanner.nextLine(); String trailer = new String(); String trailerPart = new String(); String temp = null; String tempHeaderPart = header.substring(0, 4);
// to find trailer
while ((temp = br.readLine()) != null) {
trailer = temp;
}
// getting first 4 bytes of trailer
if (trailer.trim().length() > 0) {
trailerPart = trailer.substring(0, 4);
}
// to buffer the PrintWriter's output to the file.
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
outputPath + ".txt"));
// writing the header to the file.
bufferedWriter.write(header + NEWLINE);
StringBuilder strBuilder = new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String temp1 = line.substring(0, 4);
try {
if (tempHeaderPart.equals(temp1)) {
scanner.nextLine();
} else if (trailerPart.equals(temp1)) {
scanner.nextLine();
} else {
strBuilder.append(line + NEWLINE);
}
} catch (Exception e) {
break;
}
}
bufferedWriter.write(strBuilder.toString());
bufferedWriter.write(trailer);
bufferedWriter.close();
}
public static void main(String ar[]) throws Exception {
FileProcess fileprocess = new FileProcess();
fileprocess .removeDuplicateHeader( "C:\\Users\\F50RJZR\\Desktop\\temp\\GEN4_ICICI.GEN4_FILE.20140906_2.txt", "C:\\Users\\F50RJZR\\Desktop\\temp\\outputFile"); } }