scanner类,java object转换为string
1 .概况
本文主要介绍如何将InputStream转换为字符串。 [ web linkurl=' http://code.Google.com/p/guava-libraries/' ] guava [/web link ],[weblinkURL='3358commons]
用Guava变换
以下是Guava转换的示例。 这里使用的是InputSupplier功能。
@Test
publicvoidgivenusingguava _ whenconvertinganinputstreamtoastring _ then correct (
throws IOException {
stringoriginalstring=random alphabetic (8;
inputstream inputstream=newbytearrayinputstream (original string.getbytes ();
inputsupplierinputsupplier=newinputsupplier (
@Override
公共输入流get input () throws IOException {
返回输入流;
}
(;
输入supplier reader supplier=
char streams.newreadersupplier (输入供应商,Charsets.UTF_8);
string text=char streams.tostring (reader supplier );
资产热(文本,equalto )原始字符串);
}
根据说明,1 .首先,将InputStream放入InputSupplier —。 这个方法很简单。 2 .然后使用InputStream读取参数。 这样可以获取字符流。 3 .最后使用Guava的CharStreams工具转换为String类型。
请注意,最后使用了CharStreams.toString。 inputStream也会关闭。
以下利用Guava转换的方法不是自动关闭inputStream :
@Test
publicvoidgivenusingguavaandjava7_ whenconvertinganinputstreamtoastring _ then correct (
throws IOException {
stringoriginalstring=random alphabetic (8;
inputstream inputstream=newbytearrayinputstream (original string.getbytes ();
字符串文本=null;
try (finalreaderreader=newinputstreamreader (inputstream ) ) )
text=char streams.tostring (reader );
}
资产热(文本,equalto )原始字符串);
}
其中CharStreams.toString方法不能自动关闭inputStream。 因此,我们使用了java7的方法进行处理。
使用Apache公共io转换
本节介绍如何使用Apache commons IO组件在此处重命名。 对于Guava,其他方法没有自动关闭InputStream。
@Test
publicvoidgivenusingcommonsio _ whenconvertinganinputstreamtoastring _ then correct (
throws IOException {
stringoriginalstring=random alphabetic (8;
inputstream inputstream=newbytearrayinputstream (original string.getbytes ();
string text=I outils.tostring (inputstream,StandardCharsets.UTF_8.name );
资产热(文本,equalto )原始字符串);
}
也可以使用字符串写入器进行转换。
@Test
publicvoidgivenusingcommonsiowithcopy _ whenconvertinganinputstreamtoastring _ then correct (
throws IOException {
stringoriginalstring=random alphabetic (8;
inputstream inputstream=newbytearrayinputstream (original string.getbytes ();
string writer writer=new string writer (;
string encoding=标准charsets.utf _8. name (;
Ioutils.copy(Inputstream,writer,encoding );
资产状态(writer.tostring )、equalto )、originalstring );
}
直接使用Java转换
以下是使用常见java转换的示例: 是inputStream和StringBuilder。
@Test
publicvoidgivenusingjava5_ whenconvertinganinputstreamtoastring _ then correct (
throws IOException {
stringoriginalstring=random alphabetic (default _ size );
inputstream inputstream=newbytearrayinputstream (original string.getbytes ();
stringbuildertextbuilder=new stringbuilder (;
读取器读取器=newbufferedreader (try ) newinputstreamreader
(inputStream,charset.forname (standard charsets.utf _8. name () () ) ) ) ) ) ) )
int c=0;
while((c=reader.read ) )!=-1 ()
textbuilder.append((char ) c;
}
}
资产质量(text builder.tostring )、原始字符串);
}
使用java的扫描仪
@Test
publicvoidgivenusingjava7_ whenconvertinganinputstreamtoastring _ then correct (
throws IOException {
stringoriginalstring=random alphabetic (8;
inputstream inputstream=newbytearrayinputstream (original string.getbytes ();
字符串文本=null;
try (扫描器扫描器=new扫描器(inputstream,StandardCharsets.UTF_8.name () ) ) }{
text=scanner.usedelimiter('\a ' ).next );
}
资产热(文本,equalto )原始字符串);
}
请注意,在此关闭扫描仪时关闭了inputStream
6 .超重
我们发现InputStream to String有很多不同的方法。 我们必须做的是在需要的时候选择我们方便使用的东西。