Question

Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt)...

Lab 10

Write a Java program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat).The number.txt file contains the following numbers:

1 2 3 4 5 6 7 8 9 0

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

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

------------------solution.java---------

import java.util.*;
import java.io.*;

public class solution
{
   public static void main(String[] args) throws Exception
   {
       File file = new File("numbers.txt"); //open text file reader
       DataOutputStream dos = new DataOutputStream(new FileOutputStream("binaryNumber.dat")); //output binary file
       Scanner sc = new Scanner(file); //text file scanner object
       int number;
       while (sc.hasNextInt()) { //check if file has more integers
           number = sc.nextInt(); //read int from text file
           dos.writeInt(number); //write to binary file
       }
       dos.close();

       boolean end_of_file = false;
       DataInputStream in = new DataInputStream(new FileInputStream("binaryNumber.dat")); //open binary file for reading
       while(! end_of_file){ //read until end of file
           try{
               number = in.readInt(); //read int and print on to screen
               System.out.print(number + " ");
           } catch(EOFException e){
               end_of_file = true;
           }
       }
       System.out.println("\n");
       in.close();
   }
}

------------------screenshot solution.java---------

-/java/read text file write bin/solution.java - Sublime Text (UNREGISTERED) ti 1) 9:34 AM * main.cpp numbers.txt binary Numbe

------------------Output--------

-/java/read text file write bin/numbers.txt - Sublime Text (UNREGISTERED) t0) 9:34 AM numbers.txt binary Number.dat solution.

9:34 AM vs@ubuntu: -/java/read text file write bin vs@ubuntu:-/java/read text file write bin$ javac solution.java vs@ubuntu:-

-/java/read text file write bin/binaryNumber.dat - Sublime Text (UNREGISTERED) ti 1) 9:34 AM * binary Number.dat x solution.j

------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity

Add a comment
Know the answer?
Add Answer to:
Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt)...
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
  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    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 7. 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...

  • Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and...

    Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    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 7. 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...

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • Write a C++ program that open the text file “numbers.txt”. (the numbers.txt looks like: ' 1....

    Write a C++ program that open the text file “numbers.txt”. (the numbers.txt looks like: ' 1. 6 8 ') Have a “output.txt” If summation of the numbers is bigger than 10, your code must write the first number inside “numbers.txt” in another file called “output.txt”. (use conditional statement for comparison)

  • Write a C++ program that open the text file “numbers.txt”. (the numbers.txt looks like: ' 1....

    Write a C++ program that open the text file “numbers.txt”. (the numbers.txt looks like: ' 1. 6 8 ') Have a “output.txt” If summation is less than 5, your code must write last number inside “numbers.txt” in “output.txt”, else it must write the number in between in “output.txt” (use conditional statement for comparison)

  • Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write...

    Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write the odd numbers 1 to 99 into the file. Close the file. Using Scanner, open the numbers.txt file and read in the numbers. Add them all up and print the total. Close the file.

  • For Java Write a program that reads a sequence of integer inputs all on one line...

    For Java Write a program that reads a sequence of integer inputs all on one line and prints: 1.)The smallest and largest of the inputs 2.)The number of odd and even inputs 3.)Cumulative totals. If the input is 1 7 2 9, the program prints 1 8 10 19. 4.)All adjacent duplicates. If the input is 1 3 3 4 5 5 6 6 6 2, the output is 3 5 6.

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • Write a C++ program that pick up a random number in text file name number.txt that...

    Write a C++ program that pick up a random number in text file name number.txt that contains numbers from 6-9. Cout the number the program picked

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