1

I have a byte array file with me which I am trying to convert into human readable. I tried below ways :

public static void main(String args[]) throws IOException
        {
            //System.out.println("Platform Encoding : " + System.getProperty("file.encoding")); 
            FileInputStream fis = new FileInputStream("<Path>"); 
            // Using Apache Commons IOUtils to read file into byte array 
            byte[] filedata = IOUtils.toByteArray(fis); 
            String str = new String(filedata, "UTF-8"); 
            System.out.println(str); 
            }

Another approach :

public static void main(String[] args) {
        File file = new File("<Path>");
        readContentIntoByteArray(file);
    }
    private static byte[] readContentIntoByteArray(File file) {
        FileInputStream fileInputStream = null;
        byte[] bFile = new byte[(int) file.length()];
        try {
            FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();
            for (int i = 0; i < bFile.length; i++) {
                System.out.print((char) bFile[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bFile;
    }

These codes are compiling but its not yielding output file in a human readable fashion. Excuse me if this is a repeated or basic question.

Could someone please correct me where I am going wrong here?

user2531569
  • 609
  • 4
  • 18
  • 36
  • "*but its not yielding output file in a human readable fashion*" - what do you consider as *human readable fashion*? Can you provide a sample input and output? And, if that's an option for you, I would highly encourage you to try `FileChannel` instead of raw `FileInputStream ` - they work magically with bytes, buffers and encodings. – Fureeish Nov 13 '19 at 22:25
  • Thanks for your response. I mean to say the data in the output file shouldn't be in binaries or any encrypted fashion - which i am terming as human readable. Sorry. I am not able to attach my array byte file here. – user2531569 Nov 13 '19 at 22:29
  • I didn't ask you to attach your array byte file. I am asking you to produce a [mre] with minimal input and expected + actual output of your attempt. Are you sure your file is in UTF-8? – Fureeish Nov 13 '19 at 22:30
  • I generated my input file as part of another code where I am writing it as UTF-8. When I opened my input file in notepad++, this is how the sample data in it looks like : "‹Ï#¬ŽïuÝPV·$'3œíéº9Ê2÷¬!5«¸Ã·TÛÈW…]i‡$©ÃZÖ,7Ê«ý›—CÀ,”Ëvfzì3€ø$·6D*š œÒ¤QzkMmJÆe™žŽø;0xÍòÞÃíµ(nÇ{þq•ÂÑ»™ëáô|Þœ‰ ÍnØ?'§ T4s~Û|tK´Óq¢ïÚ[zܭݨïüq+•]+½böÓžŒüP"ö)ße¿ ø“ÿ4>¢áV)‡ÌgèÀÔt[¥Ð©ëlœ‚GxwÂuÌ–Ä7ÑÄ áÌfÿ®I^{ ©*ŸŒø¶ƒ/ E¼­+Äc³ò,Ás$¦&«½ñ_Aµ:ÝÄ " – user2531569 Nov 13 '19 at 22:33
  • Shouldn't `fileInputStream.read(bFile);` result in a NPE here as you are setting it to `null` by doing `FileInputStream fileInputStream = null;`? – user2004685 Nov 13 '19 at 22:35
  • 1
    And what should be the *human readable* version of the above code you showed us? Please, for the last time, show us the sample binary file content and the expected output for that. – Fureeish Nov 13 '19 at 22:40
  • 1
    Something's fishy. It might help if we could see the code that generated the input file. – dnault Nov 13 '19 at 22:43
  • 1
    Is the file a text file or a binary file? If the former, wrap the `FileInputStream` in a `Reader`, specifying the appropriate encoding, and read the data as characters. If the latter then there isn't really a human readable form, except maybe as hex values. – Slaw Nov 13 '19 at 22:43
  • after I run i am not getting any NPE error. – user2531569 Nov 13 '19 at 22:44
  • Hi Slaw, Let me try as you said – user2531569 Nov 13 '19 at 22:45
  • @Slaw binary file may still contain data that, given appropriate encoding, can be perfectly readable as text. Why then save it to a binary file? No idea, but the point stands. – Fureeish Nov 13 '19 at 22:46
  • @Fureeish Ultimately, everything is binary. But text files are specifically the binary of, well... text. "Pure" binary files may use certain values which also have text representations but are typically only interpreted as numbers. There's a difference. – Slaw Nov 13 '19 at 22:48
  • @Slaw I completely agree, but the case I mentioned might be the one of OP's. – Fureeish Nov 13 '19 at 22:49

1 Answers1

0

Your code (from the first snippet) for decoding a byte file into a UTF-8 text file looks correct to me (assuming FileInputStream fis = new FileInputStream("Path") is yielding the correct fileInputStream) .

If you're expecting a text file format but are not sure which encoding the file format is in (perhaps it's not UTF-8) , you can use a library like the below to find out.

https://code.google.com/archive/p/juniversalchardet/

or just explore some of the different Charsets in the Charset library and see what they produce in your String initialization line and what you produce:

new String(byteArray, Charset.defaultCharset()) // try other Charsets here.

The second method you show has associated catches with byte to char conversion , depending on the characters, as discussed here (Byte and char conversion in Java). Chances are, if you cannot find a valid encoding for this file, it is not human readable to begin with, before byte conversion, or the byte array file being passed to you lost something that makes it decodeable along the way.