0

I have the following method, but this one has a problem and it is that in a part of the file there are some special characters where the character with hexadecimal value 81(.) when the program generates the export of the file is 3F(?) and with that it damages the binary:

public static void exportbinary() {
        List<String> files = listFiles(source,1);
        System.out.println("Reviewing files to extract data");
        String registro = null;
        String contenido = "";
        for (String s : files) {
            //The name of the files to be processed is adjusted
            String ogName=s;
            System.out.println("Renaming files to extract binary data...");
            File sFile = new File(source, s);
            sFile.renameTo(new File(source,s+"ext"));
            s = s+"ext";
            
            //Start binary extraction process
            File sourceFile = new File(source, s);
            File destFile = new File(destination, ogName);
            if (destFile.exists()) {
                destFile.delete();
            }
            if (!dctmp.exists()) {
                try {
                    dctmp.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Writer w = null;
            try {
                w = new BufferedWriter(new FileWriter(dctmp, true));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println("Processing" + sourceFile + " en " + destFile);
            try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
                String data;
                while ((data = br.readLine()) != null) {
                    String line = data;
                    if (line.substring(0, 6).equals("924007")) {
                        // Valores binarios
                        String binarios = line.substring(585, 586 + 82);
                        line = line.substring(0, 585) + (new String(new char[83]).replace('\0', '0'))
                                + line.substring((586 + 82), line.length());
                        // TID
                        String TID = line.substring(1005, 1020);
                        registro = TID + ";" + binarios + "\n";
                        contenido = contenido + registro;
                    }
                    BufferedWriter wrtr = new BufferedWriter(new FileWriter(destFile, true));
                    wrtr.append(line + "\n");
                    wrtr.close();
                    line = null;
                }
                System.out.println("Data is added to the temporary");
                w.append(contenido);
                w.close();
                br.close();
                System.out.println("Successfully complete process");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                sourceFile.delete();
            }
        }
    }

I did an exercise treating the file with FileInputStream() and it worked and kept the value, and it is the following code:

private void testBinary(File filename) {
   try {

     FileInputStream FileOrigin = new FileInputStream(filename);

     // Arrays
     byte[] biyeFile = new byte[(int) filename.length()];
     int[] intArray = new int[biyeFile.length];
     char[] CharArrayBit = new char[intArray.length];

     FileOrigin.read(biyeFile);

     for (int i = 0; i < biyeFile.length; i++) {
       // Converting each char into its byte equivalent
       intArray[i] = (int) Byte.toUnsignedInt(biyeFile[i]);
     }

     File temp = new File("C:/MOVIMIENTO/ebcdic/ascii/", filename.getName() + ".TMP");

     temp.createNewFile();
     FileOutputStream fileOutputStream = new FileOutputStream(temp);

     for (int j = 0; j < charArrayBit.length; ++j) {

       charArrayBit[j] = (char) intArray[j];

       try {
         if ((intArray[j] != 10) && (intArray[j] != 13)) {
           fileOutputStream.write(charArrayBit[j]);
         }
       } catch (IOException e) {
         e.printStackTrace();
       }

     }

     FileOrigin.close();
     fileOutputStream.close();
     filename.delete();
     temp.renameTo(filename);
   } catch (IOException e) {
     e.printStackTrace();
   }

 }

But I can't seem to implement the logic I made with the one that is already there and I'm getting stuck :( on how I can deal line by line in bytes. Try this Java - Read line using InputStream

The mess I have is that by using String I already lose the value String data; data = br.readLine() String line = data;

Update: The idea is that the temporary file where I am printing the TID and binary is as follows: file dc.tmp

200232133:..3....¢...€ . ...  
300232133:.....Þù...€ ....€  
400232133:ù....Þù...€ ....€ ....
etc

This is some VB.net code that I'm basing on pulling out the binary:

While POS < inputFile.Length
            While reg < rowlen
                dato1 = inputFile.ReadByte()
                POS = POS + 1
                reg = reg + 1
                dato11 = BitConverter.GetBytes(dato1)
                If (binary  = 1 And (reg > 589 And reg <= 589 + 53 + 53)) Then
                    ' here you should read two bytes and convert it to one
                    byte1 = dato1
                    ' now read the second byte
                    byte2 = inputFile.ReadByte()
                    reg = reg + 1
                    POS = POS + 1
                    final = CInt("&H" & (Chr(byte1) + Chr(byte2)))
                Else
                    dato2 = Encoding.Convert(source, DEST, dato11)
                    final = dato2(0)
                End If
                Print(1, Chr(final))
                If (reg < 7) Then 'the first positions give us the type of message
                    MTI = MTI + Chr(dato1)
                End If
                If reg = 7 Then
                    binary = 0
                    rowlen = 1400
                    If (MTI = "924007") Then ' binary
                        binary  = 1
                        rowlen = 1400 + 53
                    End If
                    MTI = ""
                End If
            End While
            POS = POS + 2
            reg = 0
        End While

Thank you.

  • 6
    binary files don't have a notion of lines, and they shouldn't not be processed using the character-oriented classes and methods of Java, e.g. don't use Readers to read them or Writers to write them. – President James K. Polk Oct 05 '22 at 23:48
  • What format does the file have? What is the objective of this method? – Tim Moore Oct 06 '22 at 00:26
  • 1
    It has no format, I can see it with Notepad, only the file arrives with the Julian date file.JJJ @TimMoore – Edisson Gabriel López Oct 06 '22 at 00:47
  • What is the file for, and what are you trying to do with it? – Tim Moore Oct 06 '22 at 00:58
  • @TimMoore Extract the binaries to another temporary file with an id:binary example: `200232133:..3....¢...€ . ... 300232133:.....Þù...€ ....€ 400232133:ù....Þù...€ ....€ ....` – Edisson Gabriel López Oct 06 '22 at 01:05
  • What do you mean by “extract”? We’re going to need a lot more information in this question if you’d like a useful answer. Going back and forth in comments is not the best approach. – Tim Moore Oct 06 '22 at 01:18
  • Also, looking at binary files in Notepad is not a good way to understand the contents. You can try a hex editor if you want to see the numeric value of each byte, but it will probably not help you to understand the format of the data. If you don’t know how the data is formatted, there is not much of anything useful you can do with it, other than copy it directly. – Tim Moore Oct 06 '22 at 01:21
  • @TimMoore There I updated the post with a VB.net code that I am basing it on to write it in JAVA. – Edisson Gabriel López Oct 06 '22 at 01:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248583/discussion-between-tim-moore-and-edisson-gabriel-lopez). – Tim Moore Oct 06 '22 at 01:39

0 Answers0