gpt4 book ai didi

java - 尝试输出 UTF-8 文本,但不起作用

转载 作者:行者123 更新时间:2023-12-01 12:37:08 25 4
gpt4 key购买 nike

我有这个代码:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


public class Decoder {
public static void Decode() throws IOException{
String input = "";
input = readFile("C:\\Users\\Dragon\\Pictures\\Binary.txt", StandardCharsets.UTF_8);
input = input.replace(" ","");
System.out.println(input);
String output = "";
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(new File("C:\\Users\\Dragon\\Pictures\\Binary2.txt")), "UTF8"));
for(int i = 0; i <= input.length() - 8; i+=8)
{
int k = Integer.parseInt(input.substring(i, i+8), 2);
out.append((char)k);
}
out.close();
System.out.println("Your File has been saved at C:\\Users\\Dragon\\Pictures\\Binary.txt");
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

基本上我正在做的是将包含可以解码为文本的二进制文件的文本文件。它解码成功,但是当输出包含输出的文件时,所有 UTF-8 字符都被替换为“?”。为什么会发生这种情况?

编辑:输入示例:

00111111 01010000 01001110 01000111 00001101 00001010 00011010 00001010 00000000 00000000 00000000 00001101 01001001 01001000 01000100 01010010 00000000 00000000 00000000 00000001 00000000 00000000 00000000 00000001 00001000 00000110 00000000 00000000 00000000 00011111 00010101 00111111 00000000 00000000 00000000 00000001 01110011 01010010 01000111 01000010 00000000 00111111 00111111 00011100 00111111 00000000 00000000 00000000 00000100 01100111 01000001 01001101 01000001 00000000 00000000 00111111 00111111 00001011 00111111 01100001 00000101 00000000 00000000 00000000 00001001 01110000 01001000 01011001 01110011 00000000 00000000 00001110 00111111 00000000 00000000 00001110 00111111 00000001 00111111 01101111 00111111 01100100 00000000 00000000 00000000 00011000 01110100 01000101 01011000 01110100 01010011 01101111 01100110 01110100 01110111 01100001 01110010 01100101 00000000 01110000 01100001 01101001 01101110 01110100 00101110 01101110 01100101 01110100 00100000 00110100 00101110 00110000 00101110 00110011 00111111 00111111 01010000 00000000 00000000 00000000 00001101 01001001 01000100 01000001 01010100 00011000 01010111 01100011 00101000 00001000 01110011 01011011 00001011 00000000 00000100 00000000 00000001 00111111 00011110 01110011 00111111 00111111 00000000 00000000 00000000 00000000 01001001 01000101 01001110 01000100 00111111 01000010 01100000 00111111 

预期输出:

‰PNG


IHDR ĉ sRGB ®Îé gAMA ±üa pHYs à ÃÇo¨d tEXtSoftware paint.net 4.0.3Œæ—P
IDATWc(s[ ºs¾² IEND®B`‚

获得的输出:

?PNG


IHDR ? sRGB ??? gAMA ???a pHYs ? ??o?d tEXtSoftware paint.net 4.0.3??P
IDATWc(s[ ?s?? IEND?B`?

最佳答案

您的输出是二进制数据,您应该将其写入二进制文件而不是文本文件。像这样简单地创建输出:

OutputStream os = new FileOutputStream("output.png");

我用扩展名 .png 命名您的输出,因为您预期的输出看起来像 PNG 图像。

并将字节写入输出,如下所示:

for(int i = 0; i <= input.length() - 8; i += 8) {
int k = Integer.parseInt(input.substring(i, i+8), 2);
out.write(k);
}

改进:

您不必从输入字符串中删除空格,因为您可以在迭代String时跳过它们:

// If spaces are not removed:
for(int i = 0; i <= input.length() - 9; i += 9) { // NOTE THE +9
int k = Integer.parseInt(input.substring(i, i+8), 2); // AND STILL +8 HERE
out.write(k);
}

此外,您甚至不需要将整个输入文件读入内存,因为您只需按 8 或 9 字节 block 读取它,因为它们为输出文件提供了有效字节。

关于java - 尝试输出 UTF-8 文本,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25481288/

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