在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
,所以我想知道什麼是使用這兩種方法之間的區別,有什麼優點和缺點對於每一個,因爲第二個更容易和更簡單,是否還有其他更好的方法來實現這一目標?
我明白了,所以如果我只讀英文拉丁字符和數字,那麼我使用最簡單的形式沒有問題,非常感謝 –