0

I am having some difficulties to convert the ".dat" file into ".text" file in my java. I am using Apache Netbeans.

Below is the question:

Book class: This class has the following private data members:

  • int year;
  • String title;

You have to create the appropriate getter and setter methods. You need to make sure that any object of this class that can be saved without problems into a file.

FindOldest class: For the FindOldest class, you will assume that there is a file called "library.dat" which contains several Book objects. The class will have main method that reads Book objects from this file and stores up to 20 of those Book objects in an array. After doing that, the main then writes the year and the title of each Book object whose year is less than 2000 back to a text file called "oldBooks.txt". In the resulting text file the info of each Book will be on a different line. Your program must have IO exception handling that handle problems that may come in reading from the file "library.dat" and saving to "oldBooks.txt" by giving appropriate feedback to the program user in the system console.

Here is the code:

books.java

 package Question2;
 
 public class Book 
 {
     // The private instance variables
     private int year;
     private String title;

     /** Constructs a Book instance with the given author */
     public Book(int year, String title) 
     {
            this.year = year;
            this.title = title;
     }
     
     // Getters and Setters
    /** Returns the year of this book */
    public int getYear() {
       return year;
    }
    
    public int setYear() {
       return year;
    }
    
    /** Returns the year of this book */
    public String getTitle() {
       return title;
    }
    
    public String setTitle() {
       return title;
    }
    
 }

FindOldest.java

 package Question2;
 
 import java.io.FileOutputStream;
 import java.io.FileInputStream;
 import java.io.BufferedOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.util.Properties;
 import java.util.Scanner;
 import java.util.Set;
 
 public class FindOldest {
     
     static int year;
     static String title;
     
     public static void main(String[] args)
     {
         try
         {
             Scanner input = new Scanner( System.in );
             Book test = new Book(year, title);
             
             // Reading data from the same file
             DataInputStream dataIn = new DataInputStream(new FileInputStream("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\Question2\\library.dat"));
             
             //output the data to another file
             DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\oldBooks.txt"));
             
             //attach FileOutputStream to BufferedOutputStream
             BufferedOutputStream bout = new BufferedOutputStream(dataOut,1024);
             System.out.println("Enter text (@ at the end):");
             char ch;
             
             while((ch=(char)dataIn.read())!='@')
             {
                 bout.write(ch);
             }
             //close the file
             bout.close();
         }
         catch(Exception ex)
         {
             System.out.println("ERROR - System Failure! You have entered an invalid value. Please restart");
         }
    
     }      
 }

library.dat

2000 Beast
2001 Harry
2002 Master
2003 Twilight
2004 Moana
2005 Encanto
2006 Despicable
2007 Australia
2008 Gandhi
2009 Vikram
2010 Rose
2011 Love
2012 Bouquet
2013 Valentine
2014 Divorce
2015 Siblings
2016 Comic
2017 Twenty
2018 Guess
2019 Spykids
2020 Godzilla

there is no output or text file shown when i debug or run the code. but it shows program successful build.

please help me as i do not know where is the mistake!!!

6
  • Why are you reading from System.in. The question is to filter books from 2000 as the year is it not ? Commented Jun 9, 2022 at 6:39
  • i am so sorry as i really dont understand the question. so what shall i change with? Commented Jun 9, 2022 at 6:44
  • I suggest you should try a simpler approach than DataStreams. Try to work with FileInput- & FileOutputStreams directly. For example read from stream until you find a space character (or maybe the second one) and work with each individual book / year pair. Debug your read loop and see the data coming in and going out and work from there. Commented Jun 9, 2022 at 6:55
  • @eskapone, thank you very much for the tips. i understand the method you explain but i am not sure on how to imply or change it in my code. Commented Jun 9, 2022 at 7:01
  • @user207421, i am sorry but may i know where did you change the code? as i did not notice any difference in the code. Commented Jun 9, 2022 at 7:19

1 Answer 1

1

I'll try to give you a template to work from. So this is intentionally not a working program.

Please do some research on file IO with java (maybe see the lectures you've had?).

Edit: the library.dat file seems to be line based. This should make things easier for you (don't search for spaces, but for line breaks instead). Also take a look at the BufferedReader class.

public static void main(String[] args)
{
  try
  {
    File library = new File("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\Question2\\library.dat");
    File oldBooks = new File("C:\\Users\\PC027\\Documents\\NetBeansProjects\\JavaApplication3\\src\\oldBooks.txt");

    // TODO: handle possible file IO exceptions
    // e.g. file not existing, no permissions to access the file, ...

    // Reading data from the same file
    InputStream dataIn = new FileInputStream(library);

    //output the data to another file
    OutputStream dataOut = new FileOutputStream(oldBooks);

    char ch = '';
    StringBuffer data = new StringBuffer();

    while(dataIn.available() > 0)
    {
      ch = dataIn.read();
      if (ch == ' ')
      {
        // We found a space, that means we just read a year or a book title...
        // probably we should do something here.
      }
      else
      {
        // Debug and see whats building up in 'data'
        data.append(ch);
      }
    }
    
    // Close the files
    dataIn.close();
    // Whoops, didn't even use dataOut...
    dataOut.close();
  }
  // Exception has subclasses...
  catch(Exception ex)
  {
    System.out.println("ERROR - failure!");
  }
}      

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the help. but i had adjusted the code by merging some of my codes and tutorial. but the code seems to have problem where when i run the file it show "classpath scanning in progress". i have no idea what is it for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.