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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:00:19+00:00 2026-06-17T08:00:19+00:00

For large strings (60MB or so long), FileWriter is appending extra null s to

  • 0

For large strings (60MB or so long), FileWriter is appending extra nulls to the end of my files. For small strings this code works as expected.

For clarity, dat and filePath are Strings.

FileWriter fstream = new FileWriter( filePath );
fstream.write( dat );
fstream.close();

File f = new File( filePath );         
System.out.println("Data: " + dat.length() + ", File: " + f.length());

In short, under what circumstances, should the two printed values be different?

Here’s my example output:

Data: 63833144, File: 63833728

I got 584 extra nulls at the end of file for some reason. I find it reasonable that the string might be over allocated, but these shouldn’t print to file, right ? To make things worse, if I explicitly give it the length:

fstream.write(dat, 0, dat.length());

The behavior is the same. Coincidentally, if I say (dat.length() – 584), it does what I want, but only in this specific case.

Any ideas?

JDK version: 1.7.0_02

Edited: Add file types for variables (both Strings)

  • 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-17T08:00:20+00:00Added an answer on June 17, 2026 at 8:00 am

    The file length depends on encoding. This test

    System.out.println(dat.getBytes().length);
    

    will show the length in bytes after encoding, because String.getBytes will use the same encoding (default) as new FileWriter(file)

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

Sidebar

Related Questions

So, I started with this: http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Ruby Which works great for really small strings. But,
I want to initialize arbitrary large strings. It is null terminated string of characters,
Is it better to transfer small or large strings by reference in C#? I
My game stores levels in large strings. Not very long, the length is about
I am reading from database a large collections of this type List<Rows<Long,String,ByteBuffer>> I then
I have a large file(60mb) and I am reading the file into a string
I need to manipulate large strings in Java (deleting and adding the deleted chars
I have a few unit tests that require very large strings for the test
I have a large set of strings. I want to divide the strings into
I am trying to perform some composition-based filtering on a large collection of strings

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.