Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

java - Can you use one scanner object to read more than one file?

What I'm wondering is if I can create one Scanner object and with that be able to read one file, finish reading its content, then read another.

So instead of doing this:

Scanner scan = new Scanner(file);
Scanner scan2 = new Scanner(file2);

I would have something like

Scanner scan = new Scanner(file);
*Code reading contents of file*
scan = Scanner(file2);

Thanks in advance!

question from:https://stackoverflow.com/questions/65928276/can-you-use-one-scanner-object-to-read-more-than-one-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this two different ways. One is simply to make a new Scanner object, which seems to be what you want. To do that you just assign a new Scanner object to the same variable, and then you can read from the new Scanner. Something like this:

Scanner scan = new Scanner(file);
// Code reading contents of file
scan.close();
scan = new Scanner(file2);
// Code reading contents of file2
scan.close();

Now, you actually asked about using a single Scanner object to read from multiple files, so technically the above code doesn't answer your question. If you look at the documentation for Scanner, there is no method to change input sources. Thankfully, Java has a neat little class called SequenceInputStream. This lets you combine two input streams into one. It reads from the first input stream until it is completely exhausted, and then it switches to the second one. We can use this to read from one file, and then switch to a second file, all in one input stream. Here's an example of how you could do this:

// Create your two separate file input streams:
FileInputStream fis1 = new FileInputStream(file);
FileInputStream fis2 = new FileInputStream(file2);

// We want to be able to see the separation between the two files,
// so I stuck a double line separator in here (not necessary):
ByteArrayInputStream sep = new ByteArrayInputStream((System.lineSeparator() + System.lineSeparator()).getBytes());

// Combine the first file and the separator into one input stream:
SequenceInputStream sis1 = new SequenceInputStream(fis1, sep);

// Combine our combined input stream above and the second file into one input stream:
SequenceInputStream sis2 = new SequenceInputStream(sis1, fis2);

// Print it all out:
try (Scanner scan = new Scanner(sis2)) {
    scan.forEachRemaining(System.out::println);
}

This will produce something like:

Content
of
file
1

Content
of
file
2

Now you've truly only created one Scanner object, and you read input from two different files with it.

Note: I have left out all exception handling in the above code snippets to reduce boilerplate code, since the question was not explicitly about exceptions. I assume you know how to handle exceptions on your own.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...