Android,读取和操作字节的方法?

发布于 2024-10-24 12:48:16 字数 821 浏览 1 评论 0原文

我正在尝试以字节为单位读取 XML 文件并对其进行解码。

我遇到的问题是将字节缓冲区连接到字符串结果中。

如果我这样做:

output += new String(buffer);

文本在它们连接的地方被损坏。我需要插入什么样的字符才能正确连接它们?

我这样做正确吗?

我使用以下代码循环遍历文件并在存储之前操作缓冲区:

 buffer = new byte[1024];

    try {
            is = getBaseContext().getAssets().open("xml/xml.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        int r = 0;

        try {
            while(r != -1)
            {

                for(int i=0;i<buffer.length;i++)
                {
                    r = is.read(buffer);
                }

                deobfuscate(buffer);
                output += new String(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

I am trying to read an XML file in bytes and decode it.

The problem I am having is joining the byte buffers into the String result.

if I do:

output += new String(buffer);

the text is corrupted at the point where they join. What kind of character do I need to insert to join them correctly?

Am I even doing this correctly?

I am using the following code to loop through the file and manipulate the buffer before storing it out:

 buffer = new byte[1024];

    try {
            is = getBaseContext().getAssets().open("xml/xml.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        int r = 0;

        try {
            while(r != -1)
            {

                for(int i=0;i<buffer.length;i++)
                {
                    r = is.read(buffer);
                }

                deobfuscate(buffer);
                output += new String(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-10-31 12:48:16

试试这个代码,

buffer = new byte[1024];

try {
        is = getBaseContext().getAssets().open("xml/xml.xml");
    } catch (IOException e) {
        e.printStackTrace();
    }

    int count = 0, bytesRead = 0;;
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream(1024 * 2);
    try {
          bytesRead = is.read(buffer);

          while(bytesRead != -1)
          {
            deobfuscate(buffer);
            bytestream.write(buffer, 0, bytesread);
            count += bytesRead;
            bytesRead = is.read(buffer);
          }
    } catch (IOException e) {
        e.printStackTrace();
    }
    String output = new String(bytestream.tobyteArray());

try this code,

buffer = new byte[1024];

try {
        is = getBaseContext().getAssets().open("xml/xml.xml");
    } catch (IOException e) {
        e.printStackTrace();
    }

    int count = 0, bytesRead = 0;;
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream(1024 * 2);
    try {
          bytesRead = is.read(buffer);

          while(bytesRead != -1)
          {
            deobfuscate(buffer);
            bytestream.write(buffer, 0, bytesread);
            count += bytesRead;
            bytesRead = is.read(buffer);
          }
    } catch (IOException e) {
        e.printStackTrace();
    }
    String output = new String(bytestream.tobyteArray());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文