Question

Using Java how would I write a program that reads and writes from binary or text...

Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this?

Example Output:

--------------------Configuration: <Default>--------------------

Enter the file name: kenb

Choose binary or text file(b/t): b

Choose read or write(r/w): w

Enter a line of information to write to the file:

lasdklj

Would you like to enter another line? Y/N only

n

Continue? (y/n)y

Enter the file name: kenb

Choose binary or text file(b/t): b

Choose read or write(r/w): r

File contains:

lasdklj

Continue? (y/n)y

Enter the file name: kent

Choose binary or text file(b/t): t

Choose read or write(r/w): w

Enter a line of information to write to the file:

OOP

Would you like to enter another line? Y/N only

Y

Enter a line of information to write to the file:

Java, C++ not C.

Would you like to enter another line? Y/N only

n

Continue? (y/n)y

Enter the file name: kent

Choose binary or text file(b/t): t

Choose read or write(r/w):r

File contains:

OOP

Java, C++ not C.

Continue? (y/n)n

Process completed.

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

/*****************************************WriteToBinaryOrText.java******************************/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class WriteToBinaryOrText {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       char exit = ' ';
       do {
           System.out.print("Enter the file name: ");
           String fileName = scan.nextLine();
           System.out.print("Choose binary of text file(b/t): ");
           char fileType = scan.nextLine().toLowerCase().charAt(0);
           System.out.print("Choose read or write(r/w): ");
           char option = scan.nextLine().toLowerCase().charAt(0);

           if (fileType == 'b') {

               if (option == 'r') {

                   readBinaryFile(fileName);

               } else {

                   writeToBinaryFile(fileName);
               }
           } else {

               if (option == 'r') {

                   readTextFile(fileName);
               } else {

                   writeToTextFile(fileName);
               }
           }

           System.out.print("Continue? (y/n): ");
           exit = scan.nextLine().toLowerCase().charAt(0);
       } while (exit != 'n');
      
       System.out.println("Process completed.");

   }

   public static void readTextFile(String fileName) {

       try {
           Scanner scan = new Scanner(new File(fileName + ".txt"));
           System.out.println("File contains: ");
           while (scan.hasNextLine()) {

               System.out.println(scan.nextLine());
           }

           scan.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }

   }

   public static void writeToTextFile(String fileName) {

       Scanner scan = new Scanner(System.in);
       try {
           char option = ' ';
           PrintWriter pt = new PrintWriter(new File(fileName + ".txt"));
           do {
               System.out.println("Enter a line of information to write to the file:");
               String line = scan.nextLine();
               pt.println(line);
               System.out.println("Would you like to enter another line? Y/N only");
               option = scan.nextLine().toLowerCase().charAt(0);
           } while (option != 'n');
           pt.flush();
           pt.close();

       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }

   public static void readBinaryFile(String fileName) {

       fileName = fileName + ".dat";

       try {
           FileInputStream fileInputStream = new FileInputStream(fileName);

           // specify UTF-8 encoding explicitly
           InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");

           BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
           String line;
           System.out.println("File contains: ");
           while ((line = bufferedReader.readLine()) != null) {
               System.out.println(line);
           }
           bufferedReader.close();
       } catch (Exception e) {

           System.out.println(e);
       }
   }

   public static void writeToBinaryFile(String fileName) {

       Scanner scan = new Scanner(System.in);
       try {

           char option = ' ';
           FileOutputStream outputStream = new FileOutputStream(fileName + ".dat");
           do {
               System.out.println("Enter a line of information to write to the file:");
               String bytes = scan.nextLine()+"\n";
               byte[] buffer = bytes.getBytes();
               outputStream.write(buffer);
               System.out.println("Would you like to enter another line? Y/N only");
               option = scan.nextLine().toLowerCase().charAt(0);
           } while (option != 'n');
           // Always close files.
           outputStream.close();

       } catch (IOException ex) {
           System.out.println("Error writing file '" + fileName + "'");

       }
   }
}
/***********************output**********************/

Enter the file name: kenb
Choose binary of text file(b/t): b
Choose read or write(r/w): w
Enter a line of information to write to the file:
lasdklj
Would you like to enter another line? Y/N only
N
Continue? (y/n): y
Enter the file name: kenb
Choose binary of text file(b/t): b
Choose read or write(r/w): r
File contains:
lasdklj
Continue? (y/n): y
Enter the file name: kent
Choose binary of text file(b/t): t
Choose read or write(r/w): w
Enter a line of information to write to the file:
OOP
Would you like to enter another line? Y/N only
Y
Enter a line of information to write to the file:
Java, C++ not C.
Would you like to enter another line? Y/N only
n
Continue? (y/n): y
Enter the file name: kent
Choose binary of text file(b/t): t
Choose read or write(r/w): r
File contains:
OOP
Java, C++ not C.
Continue? (y/n): n

Please let me know if you have any doubt or modify the answer, Thanks :)

Add a comment
Know the answer?
Add Answer to:
Using Java how would I write a program that reads and writes from binary or text...
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
  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • 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

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix...

    JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix line editor ed. The internal copy of the file is maintained as a linked list of lines. To be able to go up and down in the file, you have to maintain a doubly linked list. Most commands are represented by a one-character string. Some are two characters and require an argument (or two). Support the commands shown below: Command Function Go to the...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

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