Question

7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binar

  1. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts)

The number.txt file contains the following numbers:

1 2 3 4 5 6 7 8 9 0

IN JAVA PLZ

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileHandling {
   public static void main(String[] args) throws IOException {
       // check if File exists or not
       Scanner in = null;
       in = new Scanner(new File("number.txt"));
       int n;
       FileOutputStream fos = null;
       BufferedOutputStream writer = new BufferedOutputStream(fos);
       fos = new FileOutputStream("binaryNumber.dat");
       writer = new BufferedOutputStream(fos);
       // read from Scanner till the end of file
       while (in.hasNextInt()) {
           n = in.nextInt();
           writer.write(Integer.toBinaryString(n).getBytes());
           writer.write(" ".getBytes());
       }
       writer.flush();

       // close the file
       in.close();
       writer.close();
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT