Java文件到二进制的转换
如何将文件转换为二进制文件?我只是在我的项目中需要它。我需要通过二进制文件对文件进行加密。如果您指的是访问实际的二进制形式,请读入文件并将每个字节转换为二进制表示形式 编辑: 下面是一些将字节转换为带位字符串的代码:Java文件到二进制的转换,java,file,binary,io,Java,File,Binary,Io,如何将文件转换为二进制文件?我只是在我的项目中需要它。我需要通过二进制文件对文件进行加密。如果您指的是访问实际的二进制形式,请读入文件并将每个字节转换为二进制表示形式 编辑: 下面是一些将字节转换为带位字符串的代码: String getBits(byte b) { String result = ""; for(int i = 0; i < 8; i++) result += (b & (1 << i)) == 0 ? "0" : "1
String getBits(byte b)
{
String result = "";
for(int i = 0; i < 8; i++)
result += (b & (1 << i)) == 0 ? "0" : "1";
return result;
}
String content = "";
for(byte b : fileData)
content += getBits(b);
// content now contains your bits.
要使用这两段代码,您现在可以在每个字节上循环并创建一个字符串对象(比原始文件大小大8倍!!),其位为:
String getBits(byte b)
{
String result = "";
for(int i = 0; i < 8; i++)
result += (b & (1 << i)) == 0 ? "0" : "1";
return result;
}
String content = "";
for(byte b : fileData)
content += getBits(b);
// content now contains your bits.
使用,您可以从文件中获取字节
从JavaDoc:
FileInputStream从文件中的文件获取输入字节
系统。哪些文件可用取决于主机环境
FileInputStream用于读取原始字节流,例如
图像数据。为了阅读字符流,请考虑使用
文件阅读器
编码/解码的简单解释代码: 编码:
// Encode Character | char
Integer.toBinaryString(myByte);
要解码:
// Use BigInteger insted of Integer beacause it can decode large binary code(2)
new BigInteger(data, 2).toByteArray()
编码文件:
private static void encodeToBinary(File inputPath, File outputPath) throws IOException {
// Read all the bytes from the input file
InputStream inputData = new FileInputStream(inputPath);
ByteArrayOutputStream fileData = new ByteArrayOutputStream();
inputData.transferTo(fileData);
// StringJoiner to store binary code(2) encoded
StringJoiner binaryData = new StringJoiner(" ");
// Convert every byte into binaryString
for(Byte data : fileData.toByteArray()) {
binaryData.add(Integer.toBinaryString(data));
}
// (File)OutputStream for writing binary code(2)
OutputStream outputData = new FileOutputStream(outputPath);
outputData.write(binaryData.toString().getBytes());
// [IMPORTANT] Close all the streams
fileData.close();
outputData.close();
inputData.close();
}
private static void decodeToFile(File inputPath, File outputPath) throws IOException {
// -->>Just reverse the process
// Read all the bytes from the input (binary code(2)) file to string
InputStream inputData = new FileInputStream(inputPath);
ByteArrayOutputStream fileData = new ByteArrayOutputStream();
inputData.transferTo(fileData);
// ByteArrayOutputStream to store bytes decoded
ByteArrayOutputStream originalBytes = new ByteArrayOutputStream();
// Convert every binary code(2) to original byte(s)
for(String data : new String(fileData.toByteArray()).split(" ")) {
originalBytes.write(new BigInteger(data, 2).toByteArray());
}
// (File)OutputStream for writing decoded bytes
OutputStream outputData = new FileOutputStream(outputPath);
outputData.write(originalBytes.toByteArray());
// [IMPORTANT] Close all the streams
inputData.close();
fileData.close();
originalBytes.close();
outputData.close();
}
解码文件:
private static void encodeToBinary(File inputPath, File outputPath) throws IOException {
// Read all the bytes from the input file
InputStream inputData = new FileInputStream(inputPath);
ByteArrayOutputStream fileData = new ByteArrayOutputStream();
inputData.transferTo(fileData);
// StringJoiner to store binary code(2) encoded
StringJoiner binaryData = new StringJoiner(" ");
// Convert every byte into binaryString
for(Byte data : fileData.toByteArray()) {
binaryData.add(Integer.toBinaryString(data));
}
// (File)OutputStream for writing binary code(2)
OutputStream outputData = new FileOutputStream(outputPath);
outputData.write(binaryData.toString().getBytes());
// [IMPORTANT] Close all the streams
fileData.close();
outputData.close();
inputData.close();
}
private static void decodeToFile(File inputPath, File outputPath) throws IOException {
// -->>Just reverse the process
// Read all the bytes from the input (binary code(2)) file to string
InputStream inputData = new FileInputStream(inputPath);
ByteArrayOutputStream fileData = new ByteArrayOutputStream();
inputData.transferTo(fileData);
// ByteArrayOutputStream to store bytes decoded
ByteArrayOutputStream originalBytes = new ByteArrayOutputStream();
// Convert every binary code(2) to original byte(s)
for(String data : new String(fileData.toByteArray()).split(" ")) {
originalBytes.write(new BigInteger(data, 2).toByteArray());
}
// (File)OutputStream for writing decoded bytes
OutputStream outputData = new FileOutputStream(outputPath);
outputData.write(originalBytes.toByteArray());
// [IMPORTANT] Close all the streams
inputData.close();
fileData.close();
originalBytes.close();
outputData.close();
}
文件已经是二进制数据。请提供更多信息。你说的将文件转换成二进制是什么意思?如果你说的是文件到字节数组:我需要制作一个可以加密任何类型文件的程序。根据我们的建议,我们将对文件进行二进制加密。然后看我的帖子。这是你想要的。同时丢弃任何字符串内容。。。现在无关紧要了。你认为我能扭转它吗。文件的二进制表示形式。对于xml,只需更改为log.xml