1

我正在尝试理解 java 中的扫描仪类,有没有人知道为什么这段代码只打印第一部分的第一行而不是第二部分的所有信息。

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();
}

}

我尝试使用分隔符但没有成功。

4

2 回答 2

2

如果您运行调试器并逐步执行该getNextEntry方法,您将在第二次调用时看到,Scanner抛出一个NoSuchElementException. 发生这种情况的原因是因为第一个Scanner消耗了ByteArrayInputStream.

您可以做的一种修复是在循环Scanner之前创建并传入to而不是传入.whileScannergetNextEntryByteArrayInputStream

public static String getNextEntry(Scanner sin) {

...

Scanner sin = new Scanner(in);
String entry;
while ((entry = getNextEntry(sin)) != null) {

就第 2 部分而言,PrintWriter没有被刷新。如果您添加pout.flush()到底部putNextEntry应该会导致数据被发送到您的文件。对于对象,只有调用、、或PrintWriter时才会刷新数据。printlnprintfformatflush

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减少创建的资源,您可以在多次调用后刷新所有端。

于 2013-01-24T06:20:29.203 回答
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();
}
} 
于 2013-01-24T06:24:01.210 回答