![](/img/trans.png)
[英]How to convert 8 character string into a single byte at a time to write to file in Java?
[英]How to compare read byte from file to single character string
我有一个文本文件,我需要检查某个位置是否有"1"
或"0"
。 请注意,这是我正在检查的数字的字符串表示形式。 我试过这个:
RandomAccessFile dictionaryFile = new RandomAccessFile("path");
if("1".equals(new String(dictionaryFile.read()))){
// do stuff
}
但结果是:
ir/PersistentHashedIndex.java:322: error: no suitable constructor found for String(int)
while("1".equals(new String(dictionaryFile.read()))){
^
constructor String.String(String) is not applicable
(argument mismatch; int cannot be converted to String)
constructor String.String(char[]) is not applicable
(argument mismatch; int cannot be converted to char[])
constructor String.String(byte[]) is not applicable
(argument mismatch; int cannot be converted to byte[])
constructor String.String(StringBuffer) is not applicable
(argument mismatch; int cannot be converted to StringBuffer)
constructor String.String(StringBuilder) is not applicable
(argument mismatch; int cannot be converted to StringBuilder)
在我看来, String
需要一个字节数组而不仅仅是一个字节来初始化字符串。 但我只想给它一个数字。 我怎样才能做到这一点? 我可以将"1"
转换为其字节表示吗?
编辑:抱歉比较错误,伙计们,我把它整理出来了。 同样的问题仍然存在。
没有找到适合 String(int) 的构造函数
这意味着你整数值传递给String
的构造函数,而,没有任何构造函数String
,它接受int
值。 如果dictionaryFile.read()
返回int
。 那么,你可以做
if (dictionaryFile.read() == 1)
{
==编辑==
如果您被迫作为字符串进行比较,那么您只需向其中添加空字符串。
String temp = dictionaryFile.read()+"";
if ("1".equals(temp))
{
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.