Count number of lines in txt file java




















Append Text to an Existing File. Convert File to byte array and Vice-Versa. Create Directories. Get all Files Present in a Directory. Related Topics Java Scanner Class. Java FileInputStream Class. Java BufferedInputStream Class. Java Program to Count number of lines present in the file In this example, we will learn to count the number of lines present in a file in Java. BufferedReader package. It creates a buffering character-input stream that uses a default-sized input buffer.

Skip to content. Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. String line;. The reason the scanner class does this is because scanner. By default the delimiter is whitespace. So what's happening is that when you call scanner.

When a file ends in a newline character, there is no token after that, so hasNext and hasNextLine return false. As far as I can tell, to do what you want you'd need to count the number of delimiters, which is better handled in Martinus's answer.

See also AFinkelstein's answer. See Scanner. There is also a Scanner. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Java scanner count lines in file Ask Question. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings.



0コメント

  • 1000 / 1000