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 7652783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:51:31+00:00 2026-05-31T11:51:31+00:00

Is it a good practice to store a large string (over 300000 chars) in

  • 0

Is it a good practice to store a large string (over 300000 chars) in a sole String object in case of memory management and performance or there’s better way?

  • 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-31T11:51:32+00:00Added an answer on May 31, 2026 at 11:51 am

    Johan Sjöberg’s explanation is good:

    “It’s how to use the String which would determine the practice.”

    If 300,000 characters that make up your String object don’t reside in a Java source file enclosed in double quotes(” “), e.g. residing in an external file, then you can use different techniques to get the String content chunk by chunk or as a whole. Take a look at this example:

    FileInputStream fis = new FileInputStream( "yourFile.txt" );
    BufferedInputStream bis = new BufferedInputStream( fis );
    byte [] buffer = new byte [ 8 ];
    int bytesRead = 0;
    String chunk = "";
    
    while ( ( bytesRead = bis.read( buffer ) ) != -1 )
        chunk = new String( buffer, 0, bytesRead );
    

    Assuming you handle possible exceptions, in the code above you get a string of size 8 bytes every time you enter the while loop. So by modifying the byte array size which is 8 in this code, you can change the chunk size and use chunks anywhere else, print them etc.

    In your example, you say your string has around 300000 characters. So setting the size of the byte array buffer to 4 x 300,000 = 1,200,000 will enable you get a single string chunk that contains all the characters.

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

Sidebar

Related Questions

Is that a good practice to store the objects of the domain model in
Where is NSDefaults stored on iphone? Is it a good practice to store UserName
Is there any good practice related to dynamic_cast error handling (except not using it
Is it considered good practice to store passwords in a Github private repository? [Related
Is it a good practice to store error messages in SESSION ? For example
Is it a good practice/safe to temporarily store the HttpServletRequest and the HttpServletResponse as
I have a little quandary over whether it is good practice to do the
Is it good practice to store sub-collections of item X inside a parent collection
As by the Question . Is this a Good Practice to store a datable
There are some very good questions here on SO about file management and storing

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.