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

  • SEARCH
  • Home
  • 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 8152345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:41:08+00:00 2026-06-06T15:41:08+00:00

I have a log file that I am reading to a string public static

  • 0

I have a log file that I am reading to a string

public static String read (String path) throws IOException {
   StringBuilder sb = new StringBuilder();
   FileInputStream fs = new FileInputStream(path);
   InputStream in = new BufferedInputStream(fs);

   int r;
   while ((r = in.read()) != -1) {
       sb.append((char)r);
   }

   fs.close();
   in.close();

   return sb.toString();
}

Then I have a parser that iterates over the entire string once

void parse () {
   String con = read("log.txt");
   for (int i = 0; i < con.length; i++) {
       /* parsing action */
   }
}

This is hugely a waste of cpu cycles. I loop over all the content in Read. Then I loop over all the content in Parse. I could just place the /* parsing action */ under the while loop in the Read method, which would be find but I don’t want to copy the same code all over the place.

How can I parse the file in one iteration over the contents and still have separate methods for parsing and reading?

In C# I understand there is some sort of yield return thing, but I’m locked with Java.

What are my options in Java?

  • 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-06-06T15:41:10+00:00Added an answer on June 6, 2026 at 3:41 pm

    This is hugely a waste of cpu cycles. I loop over all the content in Read. Then I loop over all the content in Parse. I could just place the /* parsing action */ under the while loop in the Read method, which would be find but I don’t want to copy the same code all over the place.

    It’s worse than just a huge waste of cpu cycles. It’s a huge waste of memory to read the entire file into a string, if you’re only going to use it once and the use is looking at one character at a time moving forward, as your code indicates. And if your file is large, you’ll exhaust memory.

    You should parse as you read, and never have the entire file loaded into memory at once.

    If the parsing action needs to be called from more than one place, make it a function and call it rather than copying the same code all over the place. Copying a single-line function call is fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a log file in SqlServer that stores the time an application started,
I have a transaction log file in CSV format that I want use to
I have one requirement that i have to place the log file in the
I have a command (cmd1) that greps through a log file to filter out
I have a WCF service that is responsible for writing a log file. I
I have a script that runs at evening, it writes to a log file
I have a file called error.log on my server that I need to frequently
Guys i have a text file in sdcard i need to read that file.
I have a program that should read from a file.It reads some amount of
I have to read the tomcat log file,and after some time(for example:one hour) I

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.