我试图理解java中的扫描器类,是不是为什么这个代码只打印第一部分中的第一行而不是第2部分中的所有信息。
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class BadParse {
public static String getNextEntry(InputStream in) {
Scanner sin = new Scanner(in);
try {
String ssn = sin.next();
String name = sin.next();
int age = sin.nextInt();
return name + "(" + ssn + ") is " + age + " years old.";
} catch (NoSuchElementException e) {
return null;
}
}
public static void putNextEntry(String ssn, String name, int age, OutputStream out) {
PrintWriter pout = new PrintWriter(new OutputStreamWriter(out));
pout.print(ssn + " ");
pout.print(name + " ");
pout.print(age + " ");
}
public static void main(String[] args) throws IOException {
// Part I
String input = "1234567890 John 20\n0987654321 Beth 18\n2468101214 Jack 19\n";
InputStream in = new ByteArrayInputStream(input.getBytes());
String entry;
while ((entry = getNextEntry(in)) != null) {
System.out.println(entry);
}
// Part II
OutputStream out = new FileOutputStream("data");
putNextEntry("1234567890", "John", 20, out);
putNextEntry("0987654321", "Beth", 18, out);
putNextEntry("2468101214", "Jack", 19, out);
out.close();
}
}
我尝试使用分隔符,但没有成功。
答案 0 :(得分:2)
如果您运行调试器并逐步执行getNextEntry
方法,则会在第二次调用时看到Scanner
抛出NoSuchElementException
。发生这种情况的原因是因为第一个Scanner
消耗了部分ByteArrayInputStream
。
您可以执行的一项修复是在Scanner
循环之前创建while
并将Scanner
传递给getNextEntry
,而不是传递ByteArrayInputStream
。
public static String getNextEntry(Scanner sin) {
...
Scanner sin = new Scanner(in);
String entry;
while ((entry = getNextEntry(sin)) != null) {
就第2部分而言,PrintWriter
未被刷新。如果您将pout.flush()
添加到putNextEntry
的底部,这会导致数据发送到您的文件。对于PrintWriter
个对象,只有在您拨打println
,printf
,format
或flush
时才会刷新数据。
public static void putNextEntry(String ssn, String name, int age, OutputStream out) {
PrintWriter pout = new PrintWriter(new OutputStreamWriter(out));
pout.print(ssn + " ");
pout.print(name + " ");
pout.print(age + " ");
pout.flush();
}
我建议创建并传入类似于扫描仪的PrintWriter。减少创建的资源,您可以多次调用putNextEntry
后清除所有资源。
答案 1 :(得分:2)
问题是您在Scanner
内创建了getNextEntry
对象,当该对象被销毁时,它会关闭该流。尝试将Scanner作为参数传递而不是像流一样(对于PrintWriter也是如此):
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class BadParse {
public static String getNextEntry(Scanner sin) {
try {
String ssn = sin.next();
String name = sin.next();
int age = sin.nextInt();
return name + "(" + ssn + ") is " + age + " years old.";
} catch (NoSuchElementException e) {
return null;
}
}
public static void putNextEntry(String ssn, String name, int age, PrintWriter pout) {
pout.print(ssn + " ");
pout.print(name + " ");
pout.print(age + " ");
}
public static void main(String[] args) throws IOException {
// Part I
String input = "1234567890 John 20\n0987654321 Beth 18\n2468101214 Jack 19\n";
InputStream in = new ByteArrayInputStream(input.getBytes());
Scanner sin = new Scanner(in);
String entry;
while ((entry = getNextEntry(sin)) != null) {
System.out.println(entry);
}
// Part II
OutputStream out = new FileOutputStream("data");
PrintWriter pout = new PrintWriter(new OutputStreamWriter(out));
putNextEntry("1234567890", "John", 20, pout);
putNextEntry("0987654321", "Beth", 18, pout);
putNextEntry("2468101214", "Jack", 19, pout);
pout.close();
}
}