简体   繁体   English

java 数组字节文件到人类可读

[英]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.这些代码正在编译,但没有以人类可读的方式生成 output 文件。 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).您用于将字节文件解码为 UTF-8 文本文件的代码(来自第一个片段)对我来说看起来是正确的(假设 FileInputStream fis = new FileInputStream("Path") 产生正确的 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.如果您希望使用文本文件格式,但不确定文件格式采用哪种编码(可能不是 UTF-8),您可以使用如下所示的库来查找。

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:或者只是探索 Charset 库中的一些不同的字符集,看看它们在你的字符串初始化行中产生了什么以及你产生了什么:

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 ).您展示的第二种方法将捕获与字节到字符转换相关联,具体取决于字符,如此处所讨论的( 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.很有可能,如果您找不到此文件的有效编码,则在字节转换之前,它一开始就不是人类可读的,或者传递给您的字节数组文件丢失了一些使其在此过程中可解码的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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