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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:24:42+00:00 2026-06-18T23:24:42+00:00

Take the following static method: public static String fileToString(String filename) throws Exception { FileInputStream

  • 0

Take the following static method:

public static String fileToString(String filename) throws Exception {
        FileInputStream fis = new FileInputStream(filename);
        byte[] buffer = new byte[8192];
        StringBuffer sb = new StringBuffer();   
        int bytesRead; // unused? weird compiler messages...
        while((bytesRead = fis.read(buffer)) != -1) { // InputStream.read() returns -1 at EOF
            sb.append(new String(buffer));
        }
        return new String(sb);
    } 

As you can see everything looks okay, and it is perfect for small text files. But once you get to big files with thousands of lines, you encounter problems with repeating text. Based on my intuition, I thoughtbyte[] buffer was “unclean”, so to speak. So I added the following line to the method:

buffer = new byte[8192];

So that it is now:

public static String fileToString(String filename) throws Exception {
    FileInputStream fis = new FileInputStream(filename);
    byte[] buffer = new byte[8192];
    StringBuffer sb = new StringBuffer();   
    int bytesRead; // unused? weird compiler messages...
    while((bytesRead = fis.read(buffer)) != -1) { // InputStream.read() returns -1 at EOF
        sb.append(new String(buffer));
        buffer = new byte[8192]; // added new line here
    }
    return new String(sb);
} 

And it’s perfect, except for the fact that at the end of the String that the static method returns, I get a lot of null characters (depends on the buffer size). What’s going on here?

  • 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-18T23:24:43+00:00Added an answer on June 18, 2026 at 11:24 pm

    actually: // unused? weird compiler messages...

    is not weird. You never read this.

    how could sb.append(new String(buffer)); know how many bytes are written to the buffer.

    Exactly, this is where bytesRead comes into play.

    So you need new String(bytes, offset, length)

    public static String fileToString(String filename) throws Exception {
        FileInputStream fis = new FileInputStream(filename);
        byte[] buffer = new byte[8192];
        StringBuffer sb = new StringBuffer();   
        int bytesRead; // unused? weird compiler messages...
        while((bytesRead = fis.read(buffer)) != -1) { // InputStream.read() returns -1 at EOF
            sb.append(new String(buffer,0,bytesRead));
            buffer = new byte[8192];
            bytesRead=0;
        }
        return new String(sb);
    } 
    

    might work

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

Sidebar

Related Questions

Take the following code: public static string ReverseIt(string myString) { char[] foo = myString.ToCharArray();
Take the following example, I have a class public class SomeItem { public string
Take the following property: public string Foo { get; private set; } Using reflection,
I have the following extension method public static class ListExtensions { public static IEnumerable<T>
does somebody know whats wrong with the next method? public static String getMessageBundleString(String key,
I'm writing some Enum functionality, and have the following: public static T ConvertStringToEnumValue<T>(string valueToConvert,
I am updating the data by the following method class xxxxxx { public static
How can I pass an array of filenames into the following method: public static
I have the following code: private static HashSet<SoloUser> soloUsers = new HashSet<SoloUser>(); public void
I have the following static function in c# public static string Greet(string name) {

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.