Write a program that reads and writes from binary or text files. Example Output:
ID: 3907464 • Letter: W
Question
Write a program that reads and writes from binary or text files.
Example Output:
--------------------Configuration: --------------------
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.
Explanation / Answer
Code in Java:
import java.io.*;
import java.util.Scanner;
public class Binary implements Serializable{
public static void main(String[] args) {
boolean dofile = true;
boolean dofile1 = true;
String yn = "n", fname = "", bt = "b", rw = "r", line = "", readBinary = "";
PrintWriter textOut = null;
Scanner inputStream = null;
ObjectInputStream inputBinary = null;
ObjectOutputStream outputBinary = null;
OutputText mto = null;
OutputBinary mbo = null;
ObjectOutputStream binaryOut = null;
StringBuffer contents = new StringBuffer();
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("Enter the file name.");
fname = keyboard.nextLine();
System.out.println("Choose binary or text file (b/t):");
bt = keyboard.nextLine();
if(bt.equalsIgnoreCase("b")){
mbo = new OutputBinary(fname);
binaryOut = mbo.createBinaryFile();
System.out.println("Choose read or write (r/w):");
rw = keyboard.nextLine();
if (rw.equalsIgnoreCase("r")){
try{
inputBinary = new ObjectInputStream(new FileInputStream(fname + ".dat"));
readBinary = inputBinary.readUTF();
System.out.println("File contains..." + readBinary);
inputBinary.close();
}
catch (IOException e){
System.out.println("The file is blank, please write in it.");
}
}
else if(rw.equalsIgnoreCase("w")){
do{
System.out.println("Enter a line of information to write to the file:");
try{
line = keyboard.nextLine();
binaryOut.writeUTF(line);
binaryOut.flush();
binaryOut.close();
}
catch(IOException e){
System.out.println("Problem");
}
System.out.println("Would you like to enter another line? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n"))
dofile1 = false;
}while(dofile1);
}
}
else if(bt.equalsIgnoreCase("t")){
mto = new OutputText(fname);
textOut = mto.createTextFile();
System.out.println("Choose read or write (r/w):");
rw = keyboard.nextLine();
if(rw.equalsIgnoreCase("r")){
try {
inputStream = new Scanner(new FileInputStream(textOut+".txt"));
String textLine = null;
while((textLine = inputStream.nextLine()) != null){
contents.append(textLine).append(System.getProperty("line.separator"));
}
}
catch (FileNotFoundException e) {
System.out.println("Cannot find ");
}finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(contents.toString());
inputStream.close();
}
else if(rw.equalsIgnoreCase("w")){
do{
System.out.println("Enter a line to write to the file:");
line = keyboard.nextLine();
try{
textOut = mto.createTextFile();
}
catch(Exception e){
System.out.println(e);
}
textOut.println(line);
textOut.flush();
textOut.close();
System.out.println("Would you like to enter another line? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n"))
dofile1 = false;
}while(dofile1);
}
}else
System.out.println("What kind of choice is " + bt);
System.out.println("Would you like to do another file? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n"))
dofile = false;
}while(dofile);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.