Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads and writes from binary or text files. what i got so f

ID: 3544259 • Letter: W

Question

Write a program that reads and writes from binary or text files.


what i got so far


    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("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")){  
      
                        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 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);  
      
                    }  
      
                }  
                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")){  
      
                        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 file.");  
                        }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 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);  
      
                      }  
      
                    }else  
                    System.out.println("What kind of bizzare 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);  
      
       }  
      
    }  


the error it gives me


----jGRASP exec: javac -g Binary.java

Binary.java:15: error: cannot find symbol
            OutputText mto = null;  
            ^
  symbol:   class OutputText
  location: class Binary
Binary.java:16: error: cannot find symbol
            OutputBinary mbo = null;  
            ^
  symbol:   class OutputBinary
  location: class Binary
Binary.java:32: error: cannot find symbol
                    mbo = new OutputBinary(fname);  
                              ^
  symbol:   class OutputBinary
  location: class Binary
Binary.java:80: error: cannot find symbol
                    mto = new OutputText(fname);  
                              ^
  symbol:   class OutputText
  location: class Binary
4 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


im trying to figure out how to correct this

Explanation / Answer

Check this its working :



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("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")){

                   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 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);

                }

            }
           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")){

                   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 file.");
                   }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 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);

                 }

                }else
               System.out.println("What kind of bizzare 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);

   }

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote