从字符串读取和从输入流读取有什么区别?
What's the difference between reading from a string and reading from an inputstream?
在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
所以我想知道使用这两种方法有什么区别,每种方法都有什么优缺点,因为第二种方法更容易、更简单,还有没有其他更好的方法来实现这一点?
InputStream 是从资源获取信息的原始方法。它在不执行任何类型的翻译的情况下逐字节抓取数据。如果您正在读取图像数据或任何二进制文件,这是要使用的流。
另一方面,当您使用 String 时,它是针对字符序列的。您可以对字符序列使用不同的字符编码样式和解码。因此,如果您只读取文本数据或字符,那么使用 String
是可以的,但是如果您使用的是图像或任何二进制文件,那么您必须注意进一步的处理和编码.
直截了当,最好:
String sstream1 = ... // May contain Greek, Chinese, emoji, math symbols
Scanner cin2 = new Scanner(sstream1);
默认平台编码,来回转换为 Unicode 字符串。
特殊字符可能出错。不跨平台。
InputStream stream = new ByteArrayInputStream(sstream1.getBytes());
Scanner cin = new Scanner(stream);
显式、跨平台,但转换两次。
InputStream stream = new ByteArrayInputStream(sstream1.getBytes(StandardCharsets.UTF_8));
Scanner cin = new Scanner(stream, "UTF-8");
注意 System.out 也使用了默认的平台字符集,这使得测试代码无法使用。但对于所有 Unicode,扫描仅适用于第一个或最后一个代码(使用 Unicode 的 UTF-8)。
在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
所以我想知道使用这两种方法有什么区别,每种方法都有什么优缺点,因为第二种方法更容易、更简单,还有没有其他更好的方法来实现这一点?
InputStream 是从资源获取信息的原始方法。它在不执行任何类型的翻译的情况下逐字节抓取数据。如果您正在读取图像数据或任何二进制文件,这是要使用的流。
另一方面,当您使用 String 时,它是针对字符序列的。您可以对字符序列使用不同的字符编码样式和解码。因此,如果您只读取文本数据或字符,那么使用 String
是可以的,但是如果您使用的是图像或任何二进制文件,那么您必须注意进一步的处理和编码.
直截了当,最好:
String sstream1 = ... // May contain Greek, Chinese, emoji, math symbols
Scanner cin2 = new Scanner(sstream1);
默认平台编码,来回转换为 Unicode 字符串。 特殊字符可能出错。不跨平台。
InputStream stream = new ByteArrayInputStream(sstream1.getBytes());
Scanner cin = new Scanner(stream);
显式、跨平台,但转换两次。
InputStream stream = new ByteArrayInputStream(sstream1.getBytes(StandardCharsets.UTF_8));
Scanner cin = new Scanner(stream, "UTF-8");
注意 System.out 也使用了默认的平台字符集,这使得测试代码无法使用。但对于所有 Unicode,扫描仅适用于第一个或最后一个代码(使用 Unicode 的 UTF-8)。