简体   繁体   中英

java array byte file to human readable

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM