gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:18:24 24 4
gpt4 key购买 nike

所以我使用以下方法

(文件通过'convertFileToByteArray()'转换为字节数组,然后通过'convertByteArrayToBitTextFile()'写入.txt文件

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

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;
}

编辑:附加代码:

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;
}

}

我这样做是为了在非常基础的层面上尝试压缩方法,所以请我们不要讨论为什么使用人类可读的形式。

到目前为止,这工作得很好,但是我遇到了两个问题。

1)永远需要花费时间(230KB 的二进制文本需要 >20 分钟)。这只是相对复杂的转换的副产品还是有其他方法可以更快地做到这一点?

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

这仍然可能吗?或者我是否通过将文件转换为人类可读的文本文件而丢失了有关该文件应表示的内容的信息?如果是这样,您知道有什么替代方法可以防止这种情况发生吗?

感谢任何帮助。

最佳答案

花费你最多时间的事情是构造一个不断增加的字符串。更好的方法是在获得数据后立即写入。

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

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;
}

关于java - 如何将二进制文本转换为可用文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20460153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com