简体   繁体   中英

Scanner cuts off my String after about 2400 characters

I've got some very basic code like

while (scan.hasNextLine())
{
    String temp = scan.nextLine();
    System.out.println(temp);
}

where scan is a Scanner over a file.

However, on one particular line, which is about 6k chars long, temp cuts out after something like 2470 characters. There's nothing special about when it cuts out; it's in the middle of the word "Australia." If I delete characters from the line, the place where it cuts out changes; eg if I delete characters 0-100 in the file then Scanner will get what was previously 100-2570.

I've used Scanner for larger strings before. Any idea what could be going wrong?

At a guess, you may have a rogue character at the cut-off point: look at the file in a hex editor instead of just a text editor. Perhaps there's an embedded null character, or possibly \\r in the middle of the string? It seems unlikely to me that Scanner.nextLine() would just chop it arbitrarily.

As another thought, are you 100% sure that it's not all there? Perhaps System.out.println is chopping the string - again due to some "odd" character embedded in it? What happens if you print temp.length() ?

EDIT: I'd misinterpreted the bit about what happens if you cut out some characters. Sorry about that. A few other things to check:

  • If you read the lines with BufferedReader.readLine() instead of Scanner , does it get everything?
  • Are you specifying the right encoding? I can't see why this would show up in this particular way, but it's something to think about...
  • If you replace all the characters in the line with "A" (in the file) does that change anything?
  • If you add an extra line before this line (or remove a line before it) does that change anything?

Failing all of this, I'd just debug into Scanner.nextLine() - one of the nice things about Java is that you can debug into the standard libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM