I am trying to read an GZ file which has EBCDIC data and need to write the records into output file if that records has an character in a given position.
I have done the below:
Read the GZ file line by line using Buffered reader and GZInputStream, however when I read line ny line I am not able to compare the input char with the record.
I have specified the encoding as Cp1047 for the reader.
I am converting the character to ebcdic and got the byte . How can now i compare this byte with record
Can some help me out or tell me how can this be achieved?
PFB code
public static void main(String[] args) {
// TODO Auto-generated method stub
int buffersize = 1024;
File file = null;
GzipCompressorInputStream gzIn = null;
FileInputStream fin = null;
BufferedInputStream in = null;
FileOutputStream out = null;
ByteBuffer bb = ByteBuffer.allocateDirect(6* 1024 * 1024);
try {
//creating the object to access the methods in this class
ReadZipFile obj = new ReadZipFile();
// calingmethod to get the file name from src/main/resources
File result1 =obj.getFileWithUtil("txt.gz");
//declaring file input stream using file object
fin = new FileInputStream(result1);
//declaring the reader for gzip with buffered reader object
gzIn = new GzipCompressorInputStream(fin);
//delcaring buffered input stream reader
in = new BufferedInputStream(gzIn,6*1024*1024);
// declaring chaneel with buffereinput stream
ReadableByteChannel rbc = Channels.newChannel(in);
//delcarin the output file
out = new FileOutputStream("gzsample2.txt");
final byte[] buffer = new byte[buffersize];
BufferedReader bfrdr = new BufferedReader(new InputStreamReader(gzIn,"Cp1047"));
String content = null;
char logrequired = 'j'; // This is the value that i will get as job paramter
while ((content = bfrdr.readLine()) != null) {
if (logrequired == content.charAt(10)) {
System.out.println("in write cond meet:");
}
// log.info("record is :" +content);
}
//
//// int bytesRead = rbc.read(bb);
//
// int bytesRead =0;
//
// bytesRead = rbc.read(bb);
// int position = 1;
// int length = 8;
//// System.out.println(bb.asCharBuffer());
// ByteBuffer rec = bb.get(buffer, position, length);
//
// // PrintByteBuffer(bb, position, 32);
//
//to write
// while (-1 != (n = gzIn.read(buffer))) {
// out.write(buffer, 0, n);
// }
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Now close the all the readers
try {
out.close();
gzIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private File getFileWithUtil(String fileName) {
System.out.println("getFileWithUtil");
System.out.println(fileName);
// Get file from resources folder
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println(file.getAbsolutePath());
return file;
}
}