我正在尝试为一个单词的方法编写junit测试。 我有问题,该方法是返回符号,而不是depadded字。将DataInputStream设置为字符串值
我的测试方法是
@Test
public void testReadString() throws IOException
{
String testString = "******test";
InputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
DataInputStream dis = new DataInputStream(stream);
String word = readString(dis, 10);
assertEquals("test", word);
}
的方法是测试是
public static String readString(DataInputStream dis, int size) throws IOException
{
byte[] makeBytes = new byte[size * 2];// 2 bytes per char
dis.read(makeBytes); // read size characters (including padding)
return depad(makeBytes);
}
public static String depad(byte[] read)
{
//word = word.replace("*", "");
StringBuilder word = new StringBuilder();
for (int i = 0; i < read.length; i += 2)
{
char c = (char) (((read[i] & 0x00FF) << 8) + (read[i + 1] & 0x00FF));
if (c != '*')
{
word.append(c);
}
}
return word.toString();
}
当我运行测试,我得到的错误是 测试预期失败[测试]但[⨪ ⨪⨪瑥獴]
上午我从评论等正确的结论,你是读入文件到一个字节数组,治疗为UTF-8编码的文本和你它的一部分想要删除它并将剩余的文本作为字符串?如果是这样,你能解释一下吗?你确定它是UTF-8吗? –