Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:10:15+00:00 2026-05-27T10:10:15+00:00

If I want to copy the content of a text file using a string

  • 0

If I want to copy the content of a text file using a string tokenizer let’s say like this:

StringTokenizer strk = new StringTokenizer(new String(buffer), Constant.DELIMS,true);

And then I want to browse the string tokenizer but also keep count in a variable of how many lines I passed from the initial file:(by this I mean count how many tokens that represent a newline character)

nr_lines=0;
endline="\n";
while (strk.hasMoreTokens()) {
       String word = strk.nextToken(); 
       if (word.equals(endline)) nr_lines++;  
} 

But it doesn’t seem to work right. Am I making the comparison with a wrong delimiter(\n). I also tried with \r. But it returns larger numbers than it should, like nr_lines would increment when it shouldn’t. Could you please help me out. Also if you have another idea of how I could keep track of the line number after transferring the content of a file to a string tokenizer, it would be most welcomed. Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:10:16+00:00Added an answer on May 27, 2026 at 10:10 am

    I would recommend using Scanner instead. While it isn’t as useful for returning Objects, it has the nextLine method, which you can then parse through using StringTokenizer.

    Like this:

    New Scanner
    New StringTokenizer
    
    Get next line from Scanner
      Count the line
      Re-initialize StringTokenizer with the newly read line from Scanner
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to copy a binary master file in a new binary file. This
Let's say I want to generate this output: public String toString() { return this.getFirstName()
Help. TCHAR* b; TCHAR* c=TEXT(qwerty); I want to allocate memory and copy content of
I want to create a doc file and copy some of the content to
Say I want to copy the contents of a directory excluding files and folders
I have two Java.io.File objects file1 and file2. I want to copy the contents
I want to copy a file from A to B in C#. How do
I want to copy text files and only text files from src/ to dst/
I want to copy a file from a remote server to a local server
I have a RichTextBox and I want to save the text to a file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.