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!!!