Java 如何将二进制文本转换为可用文件

Java 如何将二进制文本转换为可用文件,java,binary,file-conversion,Java,Binary,File Conversion,因此,我使用以下方法 (文件通过“convertFileToByteArray()”转换为字节数组,然后通过“ConvertByteArrayToByteXTFile()”写入.txt文件。) 将任何类型的文件转换为二进制文本文件(我指的是人类可读形式的1和0) 公共静态字节[]convertFileToByteArray(字符串路径)引发IOException { 文件=新文件(路径); 字节[]文件数据; fileData=新字节[(int)file.length()]; FileInput

因此,我使用以下方法

(文件通过“convertFileToByteArray()”转换为字节数组,然后通过“ConvertByteArrayToByteXTFile()”写入.txt文件。)

将任何类型的文件转换为二进制文本文件(我指的是人类可读形式的1和0)

公共静态字节[]convertFileToByteArray(字符串路径)引发IOException
{
文件=新文件(路径);
字节[]文件数据;
fileData=新字节[(int)file.length()];
FileInputStream in=新的FileInputStream(文件);
in.read(文件数据);
in.close();
返回文件数据;
}
公共静态布尔ConvertByteArrayToBitExtFile(字符串路径,字节[]字节)
{
字符串内容=convertByteArrayToBitString(字节);
尝试
{
PrintWriter out=新的PrintWriter(路径);
out.println(内容);
out.close();
返回true;
}
catch(filenotfounde异常)
{
返回false;
}
}
公共静态字符串convertByteArrayToBitString(字节[]字节)
{
字符串内容=”;
for(int i=0;i
编辑:附加代码:

public static byte[] convertFileToByteArray(String path) throws IOException
{
    File file = new File(path);
    byte[] fileData;
    fileData = new byte[(int)file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(fileData);
    in.close();
    return fileData;
}

public static boolean convertByteArrayToBitTextFile(String path, byte[] bytes)
{
    try
    {
        PrintWriter out = new PrintWriter(path);
        for (int i = 0; i < bytes.length; i++)
        {
            out.print(String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0'));
        }
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static boolean convertByteArrayToByteTextFile(String path, byte[] bytes)
{
    try
    {
        PrintWriter out = new PrintWriter(path);
        for(int i = 0; i < bytes.length; i++)
        {
            out.print(bytes[i]);
        }
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static boolean convertByteArrayToRegularFile(String path, byte[] bytes)
{
    try
    {
        PrintWriter out = new PrintWriter(path);
        for(int i = 0; i < bytes.length; i++)
        {
            out.write(bytes[i]);
        }
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static boolean convertBitFileToByteTextFile(String path) 
{
    try
    {
        byte[] b = convertFileToByteArray(path);
        convertByteArrayToByteTextFile(path, b);
        return true;
    }
    catch (IOException e)
    {
        return false;
    }

}
公共静态字节[]convertFileToByteArray(字符串路径)引发IOException
{
文件=新文件(路径);
字节[]文件数据;
fileData=新字节[(int)file.length()];
FileInputStream in=新的FileInputStream(文件);
in.read(文件数据);
in.close();
返回文件数据;
}
公共静态布尔ConvertByteArrayToBitExtFile(字符串路径,字节[]字节)
{
尝试
{
PrintWriter out=新的PrintWriter(路径);
for(int i=0;i
我这样做是为了在一个非常基本的层面上尝试压缩方法,所以请不要讨论为什么要使用人类可读的形式

public static byte[] convertFileToByteArray(String path) throws IOException
{
    File file = new File(path);
    byte[] fileData;
    fileData = new byte[(int)file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(fileData);
    in.close();
    return fileData;
}

public static boolean convertByteArrayToBitTextFile(String path, byte[] bytes)
{
    String content = convertByteArrayToBitString(bytes);
    try
    {
        PrintWriter out = new PrintWriter(path);
        out.println(content);
        out.close();
        return true;
    }
    catch (FileNotFoundException e)
    {
        return false;
    }
}

public static String convertByteArrayToBitString(byte[] bytes)
{
    String content = "";
    for (int i = 0; i < bytes.length; i++)
    {
        content += String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0');
    }
    return content;
}
到目前为止,这一切都很好,但是我有两个问题

(一) 它需要很长时间(230KB的二进制文本超过20分钟)。这仅仅是相对复杂的转换的副产品,还是有其他方法可以更快地实现这一点

2) 主要问题: 我不知道如何将这些文件转换回原来的样子。从.txt重命名为.exe不起作用(这并不奇怪,因为生成的文件比原始文件大两倍)

这仍然是可能的,还是因为将文件转换为人类可读的文本文件而丢失了关于该文件应该表示什么的信息? 如果是这样的话,你知道有什么办法可以防止这种情况发生吗

感谢您的帮助

  • 使用
    StringBuilder
    避免生成大量未使用的
    String
    实例。
    更好的方法是直接写入
    PrintWriter
    ,而不是在内存中构建它

  • 循环遍历每个8个字符的子序列,并调用
    Byte.parseByte(text,2)
    将其解析回一个字节

  • 使用
    StringBuilder
    避免生成大量未使用的
    String
    实例。
    更好的方法是直接写入
    PrintWriter
    ,而不是在内存中构建它

  • 循环遍历每个8个字符的子序列,并调用
    Byte.parseByte(text,2)
    将其解析回一个字节


  • 花费您最多时间的事情是构造一个不断增加的字符串。一种更好的方法是,一旦有了数据,就立即编写它

    另一个问题很简单。您知道八个字符的每个序列(“0”或“1”)都是由一个字节组成的。因此,您知道8个字符块中每个字符的值:

    01001010
           ^----- 0*1
          ^------ 1*2
         ^------- 0*4
        ^-------- 1*8
       ^--------- 0*16
      ^---------- 0*32
     ^----------- 1*64
    ^------------ 0*128
                  -----
                  64+8+2 = 74
    
    只需在存在“1”的位置添加值

    您可以在Java中这样做,甚至不知道单个位值:

    String sbyte = "01001010";
    int bytevalue = 0;
    for (i=0; i<8; i++) {
        bytevalue *= 2;           // shifts the bit pattern to the left 1 position
        if (sbyte.charAt(i) == '1') bytevalue += 1;
    }
    
    String sbyte=“01001010”;
    int字节值=0;
    
    对于(i=0;i而言,花费您最多时间的事情是构造一个不断增加的字符串。更好的方法是在您拥有数据后立即写入它

    另一个问题很简单。您知道每个8个字符的序列(“0”或“1”)都是由一个字节组成的。因此,您知道8个字符块中每个字符的值:

    01001010
           ^----- 0*1
          ^------ 1*2
         ^------- 0*4
        ^-------- 1*8
       ^--------- 0*16
      ^---------- 0*32
     ^----------- 1*64
    ^------------ 0*128
                  -----
                  64+8+2 = 74
    
    只需在存在“1”的位置添加值

    您可以在Java中这样做,甚至不知道单个位值:

    String sbyte = "01001010";
    int bytevalue = 0;
    for (i=0; i<8; i++) {
        bytevalue *= 2;           // shifts the bit pattern to the left 1 position
        if (sbyte.charAt(i) == '1') bytevalue += 1;
    }
    
    String sbyte=“01001010”;
    int字节值=0;
    
    对于(i=0;iWow,这将其缩短为2秒。非常感谢!关于2):感谢您的详细解释!但是,如果我将可执行文件更改为位文本,然后使用您的方法将过程(位到字节)反转