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

The Archive Base Latest Questions

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

I have problem while writing to the file. I want to write contents of

  • 0

I have problem while writing to the file. I want to write contents of my input file to output file but while writing to the file, I am getting NULL value written at the end of file.

What’s the reason behind that?

My code is:

 import java.io.BufferedReader;
 import java.io.DataInputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;


 public class FileReading {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    FileInputStream fi=new  
  FileInputStream("E:\\Tejas\\NewData_FromNov\\New_Folder\\bb.txt");
    DataInputStream di=new DataInputStream(fi);

    BufferedReader buf=new BufferedReader(new InputStreamReader(di));
    FileOutputStream fout=new FileOutputStream("E:\\Tejas\\NewData_FromNov\\New_Folder\\Out_bb.txt");
    int ss;

    byte[] input=new byte[500];
    int len=input.length;

    while((ss=di.read(input,0,len))!=-1)
    {
        System.out.println(ss);
        //fout.write(ss);
        fout.write(input,0,len);

    }
    fout.close();

}

 }
  • 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-27T06:14:11+00:00Added an answer on May 27, 2026 at 6:14 am

    You’re always writing out the full buffer, even if you’ve only read part of it because the third argument to write is len (the length of the buffer) instead of ss (the number of bytes read). Your loop should look like this:

    int bytesRead; // Easier to understand than "ss"
    byte[] buffer = new byte[500];
    
    while((bytesRead = di.read(buffer, 0, buffer.length)) != -1)
    {
        System.out.println(bytesRead);
        fout.write(buffer, 0, bytesRead);
    }
    

    Additionally:

    • You should close both the input and output streams in finally blocks to ensure they’re always closed (even if there’s an exception).
    • You don’t need a DataInputStream – just a FileInputStream is fine here.
    • You’re not using your BufferedReader at all.
    • Consider using Guava or a similar third-party library which contains utility methods to do all of this.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a codes to write my data from one input file to
I have come across an annoying problem while writing some PHP4 code. I renamed
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
I have a problem while pasting my contents (or text) generated by Java code
I have a problem while scrolling images on tableview. I am getting a Signal
I have a flash file menu i am having problem while link it to
I am writing an Excel File using Apache POI. I want to write in
I have a MediaPlayer playing a file, and I want to continuously write logging
I have a problem while writting event for ImageUpload User control. I want to
I have this problem when I want to skip to a position while streaming

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.