Java的DataInputStream.readUTF和读取的字节数 [英] Java's DataInputStream.readUTF and number of bytes read

查看:273
本文介绍了Java的DataInputStream.readUTF和读取的字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JAVA(准确地说是J2ME)编写自定义存档格式.归档程序可以正常运行,但是我在进行归档方面几乎没有问题.

I am writing a custom archive format in JAVA (J2ME to be precise). The archiver works OK, however I have little problems with de-archiving.

当通过readUTF方法读取UTF8字符串时,如何知道已读取多少字节?我知道使用writeUTF保存的字符串的前两个字节是字符串长度的短值,但是我想要一个更好的解决方案.

How could I know how many bytes have been read when reading a UTF8 string thorough the readUTF method? I know that the first two bytes of a string saved using writeUTF are a short value of the string's length, but I'd like a nicer solution.

或者有什么方法可以知道在DataInputStream的末尾还剩下多少字节(available方法似乎没有返回有意义的值).该流是通过FileConnection(即FileConnection.openDataInputStream.

Or is there any way to know how many bytes are left till the end of the DataInputStream (the available method doesn't seem to return meaningful values). The stream was opened by a FileConnection, i.e. FileConnection.openDataInputStream.

谢谢大家!

推荐答案

我看不到直接使用CLDC API实现此目的的方法.但是,一种简单的解决方案是滚动自己的"CountingInputStream",该扩展扩展InputStream并计算读取的字节数.您基本上只需要重写这三种读取方法即可.

I can't see a way of achieving this using the CLDC API directly. A simple solution however, would be to roll your own "CountingInputStream" that extends InputStream and counts the number of bytes read. You would basically just need to override the three read-methods.

文件大小可通过FileConnection获得.

The file size is available through the FileConnection.

这篇关于Java的DataInputStream.readUTF和读取的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆