I need to create a program that reads and writes binary or text files. What I cu
ID: 674442 • Letter: I
Question
I need to create a program that reads and writes binary or text files. What I currently have is below, but my program does not read properly. I feel like I have made this way more complicated than it needed it to be. Please help simplify me into a working program!
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 = "";
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("Chose binary or text file (b/t):");
bt = keyboard.nextLine();
if(bt.equalsIgnoreCase("b")){
mbo = new OutputBinary(fname);
binaryOut = mbo.createBinaryFile();
System.out.println("Chose read or write (r/w):");
rw = keyboard.nextLine();
if (rw.equalsIgnoreCase("r")){ //My read binary file is not working.
try{
inputBinary = new ObjectInputStream(new FileInputStream(fname + ".bin"));
inputBinary.readUTF();
System.out.println("File contains..." + inputBinary);
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 with file output.");
}
System.out.println("Would you like to enter another line? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n"))
dofile1 = false;
}while(dofile1); //If the user selects no, how to make sure program ends.
//if user selects yes, have the program start over
}
}
else if(bt.equalsIgnoreCase("t")){
mto = new OutputText(fname);
textOut = mto.createTextFile();
}
System.out.println("Chose read or write (r/w):");
rw = keyboard.nextLine();
if(rw.equalsIgnoreCase("r")){ //**********My code for reading a text file is not working
try {
inputStream = new Scanner(new FileInputStream(textOut+".txt"));
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("File contains: " + textOut);
line = inputStream.nextLine();
inputStream.close();
}
else if(rw.equalsIgnoreCase("w")){
do{
System.out.println("Enter a line of information 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"))
dofile = false;
}while(dofile);
}
System.out.println("Would you like to do another file? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n"))
dofile = false;
}while(dofile);
}
}
public class MyException extends Exception{
// default constructor
public MyException(){
super("Cannot create file");
}
// set message constructor
public MyException(String s){
super(s);
}
}
import java.io.*;
public class OutputBinary extends java.lang.Exception {
private String fname = "";
public OutputBinary() {
}
public OutputBinary(String f) {
super("File Does Not Exist");
fname = f;
}
public ObjectOutputStream createBinaryFile(){
ObjectOutputStream mbo = null;
try{
mbo = new ObjectOutputStream(new FileOutputStream(fname+".bin",false));
}
catch (IOException e){
System.out.println("Error: the file could not be created.");
}
return mbo;
}
public String getFname(){
return fname;
}
public void setFname(String f){
fname = f;
}
}
import java.io.*;
public class OutputText extends java.lang.Exception {
private String fname = "";
public OutputText() {
}
public OutputText(String f) {
super("File Does Not Exist");
fname = f;
}
public PrintWriter createTextFile(){
PrintWriter pw = null;
try{
pw = new PrintWriter( new FileOutputStream(fname+".txt",false));
FileOutputStream fos = new FileOutputStream(fname+".txt",false);
}
catch (FileNotFoundException e){
System.out.println("Problem creating file.");
}
return pw;
}
public String getFname(){
return fname;
}
public void setFname(String f){
fname = f;
}
}
Write a program that reads and writes from binary or text files Example Output: Configuration:Explanation / Answer
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Scanner;
public class Binary implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
boolean dofile = true;
boolean dofile1 = true;
String yn = "n", fname = "", bt = "b", rw = "r", line = "";
PrintWriter textOut = null;
Scanner inputStream = null;
ObjectInputStream inputBinary = 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("Chose binary or text file (b/t):");
bt = keyboard.nextLine();
if(bt.equalsIgnoreCase("b")){
mbo = new OutputBinary(fname);
binaryOut = mbo.createBinaryFile();
System.out.println("Chose read or write (r/w):");
rw = keyboard.nextLine();
if (rw.equalsIgnoreCase("r")){ //My read binary file is not working.
try{
System.out.println("fname=="+fname);
FileInputStream inFile = new FileInputStream(fname + ".bin");
inputBinary = new ObjectInputStream(inFile);
while(true){
String lineInfo=inputBinary.readLine();
if(lineInfo==null){
break;
}
System.out.println(lineInfo);
}
}
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.writeBytes(line);
binaryOut.flush();
}
catch(IOException e){
e.printStackTrace();
System.out.println("Problem with file output.");
}
System.out.println("Would you like to enter another line? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n")){
dofile1 = false;
try{
binaryOut.close();
}catch (Exception e) {
// TODO: handle exception
}
}
}while(dofile1); //If the user selects no, how to make sure program ends.
//if user selects yes, have the program start over
}
}
else if(bt.equalsIgnoreCase("t")){
mto = new OutputText(fname);
textOut = mto.createTextFile();
System.out.println("Chose read or write (r/w):");
rw = keyboard.nextLine();
if(rw.equalsIgnoreCase("r")){ //**********My code for reading a text file is not working
try {
inputStream = new Scanner(new FileInputStream(fname+".txt"));
}
catch (Exception e) {
System.out.println(e);
}
while (inputStream.hasNextLine()) {
String lineInfo = inputStream.nextLine();
System.out.println(lineInfo);
}
inputStream.close();
/*System.out.println("File contains: " + textOut);
line = inputStream.nextLine();
inputStream.close();*/
}
else if(rw.equalsIgnoreCase("w")){
do{
System.out.println("Enter a line of information 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);
}
}
System.out.println("Would you like to do another file? (y/n)");
yn = keyboard.nextLine();
if(yn.equalsIgnoreCase("n"))
dofile = false;
}while(dofile);
}
}
public class MyException extends Exception{
// default constructor
public MyException(){
super("Cannot create file");
}
// set message constructor
public MyException(String s){
super(s);
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class OutputBinary extends java.lang.Exception {
private String fname = "";
public OutputBinary() {
}
public OutputBinary(String f) {
super("File Does Not Exist");
fname = f;
}
public ObjectOutputStream createBinaryFile(){
ObjectOutputStream mbo = null;
try{
File file=new File(fname+".bin");
if(file.exists()){}else{
mbo = new ObjectOutputStream(new FileOutputStream(file,true));
}
}
catch (IOException e){
System.out.println("Error: the file could not be created.");
}
return mbo;
}
public String getFname(){
return fname;
}
public void setFname(String f){
fname = f;
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.File;
public class OutputText extends java.lang.Exception {
private String fname = "";
public OutputText() {
}
public OutputText(String f) {
super("File Does Not Exist");
fname = f;
}
public PrintWriter createTextFile(){
PrintWriter pw = null;
try{
java.io.File fileName=new File(fname+".txt");
pw = new PrintWriter( new FileOutputStream(fileName,true));
FileOutputStream fos = new FileOutputStream(fileName,true);
System.out.println("file path=="+fileName.getAbsoluteFile());
}
catch (FileNotFoundException e){
System.out.println("Problem creating file.");
}
return pw;
}
public String getFname(){
return fname;
}
public void setFname(String f){
fname = f;
}
}
output:
Enter the file name.
textfile1
Chose binary or text file (b/t):
t
file path==E:SpAnWorkspaceSampleONLW extfile1.txt
Chose read or write (r/w):
w
Enter a line of information to write to the file:
hai this info1
file path==E:SpAnWorkspaceSampleONLW extfile1.txt
Would you like to enter another line? (y/n)
y
Enter a line of information to write to the file:
hai this is info2
Would you like to enter another line? (y/n)
n
Would you like to do another file? (y/n)
y
Enter the file name.
textfile1
Chose binary or text file (b/t):
t
Chose read or write (r/w):
r
hai this info1
hai this is info2
Would you like to do another file? (y/n)
y
Enter the file name.
binaryfile1
Chose binary or text file (b/t):
b
Chose read or write (r/w):
w
Enter a line of information to write to the file:
hi this is info1
Would you like to enter another line? (y/n)
n
Would you like to do another file? (y/n)
y
Enter the file name.
binaryfile1
Chose binary or text file (b/t):
b
Chose read or write (r/w):
r
fname==binaryfile1
hi this is info1
Would you like to do another file? (y/n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.