在java中我可以做到这一点从字符串读取和从输入流读取之间有什么区别?
String sstream1 =
"as aasds 2 33\n" +
"this\n" +
"2.23\n";
InputStream stream = new ByteArrayInputStream(sstream1.getBytes());
Scanner cin = new Scanner(stream);
Scanner cin2 = new Scanner(sstream1);
String x1 = cin.next();
String x2 = cin.next();
int x3 = cin.nextInt();
int x4 = cin.nextInt();
String x5 = cin.next();
double x6 = cin.nextDouble();
Stream.of(x1, x2, x3, x4, x5, x6).forEach(o -> System.out.println(o));
x1 = cin2.next();
x2 = cin2.next();
x3 = cin2.nextInt();
x4 = cin2.nextInt();
x5 = cin2.next();
x6 = cin2.nextDouble();
Stream.of(x1, x2, x3, x4, x5, x6).forEach(o -> System.out.println(o));
我仍然得到同样的结果
as
aasds
2
33
this
2.23
as
aasds
2
33
this
2.23
,所以我想知道什么是使用这两种方法之间的区别,有什么优点和缺点对于每一个,因为第二个更容易和更简单,是否还有其他更好的方法来实现这一目标?
我明白了,所以如果我只读英文拉丁字符和数字,那么我使用最简单的形式没有问题,非常感谢 –