I have an ArrayList of Objects and i want to store them into the file and also i want to read them from the file to ArrayList. I can successfully write them into the file using writeObject method but when reading from the file to ArrayList, i can only read first object. Here is my code for reading from serialized file
public void loadFromFile() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
myStudentList = (ArrayList<Student>) ois.readObject();
}
EDIT:
This is the code for writing list into the file.
public void saveToFile(ArrayList<Student> list) throws IOException {
ObjectOutputStream out = null;
if (!file.exists ()) out = new ObjectOutputStream (new FileOutputStream (file));
else out = new AppendableObjectOutputStream (new FileOutputStream (file, true));
out.writeObject(list);
}
Rest of my class is
public class Student implements Serializable {
String name;
String surname;
int ID;
public ArrayList<Student> myStudentList = new ArrayList<Student>();
File file = new File("src/files/students.txt");
public Student(String namex, String surnamex, int IDx) {
this.name = namex;
this.surname = surnamex;
this.ID = IDx;
}
public Student(){}
//Getters and Setters
public void add() {
Scanner input = new Scanner(System.in);
System.out.println("name");
String name = input.nextLine();
System.out.println("surname");
String surname = input.nextLine();
System.out.println("ID");
int ID = input.nextInt();
Ogrenci studenttemp = new Ogrenci(name, surname, ID);
myOgrenciList.add(studenttemp);
try {
saveToFile(myOgrenciList, true);
}
catch (IOException e){
e.printStackTrace();
}
}
Ok so you are storing whole list of students every time when new student comes in, so basicly what your file is keeping is:
List with one student
List with two students including the first one
List of 3 studens
and so on and so on.
I know you are probably thought it will write only new students in incremental fashion, but you were wrong here
.
You should rather add all students you want to store, into the list first. And then store complete list into the file , just like you are doing it.
Now, when you will be reading from the filre, first readObject will return you the list no.1 - that is why you are getting list with only one student. Second read would give you list no.2 and so on.
So you save your data you either have to:
Create complete list containig N students and store it once ito the file
Do not use list, but store students directly to the file
To read it back:
readObject once, so you will get List<Students>
Read students one by one from the file by multiple calls to readObject
This is Because I think ObjectOutputStream will return the first object from a file.
If you want all of the objects you can use for loop and use like this -:
FileInputStream fis = new FileInputStream("OutObject.txt");
for(int i=0;i<3;i++) {
ObjectInputStream ois = new ObjectInputStream(fis);
Employee emp2 = (Employee) ois.readObject();
System.out.println("Name: " + emp2.getName());
System.out.println("D.O.B.: " + emp2.getSirName());
System.out.println("Department: " + emp2.getId());
}
Related
How to write a constructor that holds the sorted array, Then write it to a file with a method like getDatabase that returns an object that has been passed the sorted array.
Database class:
public Person[] entry; // this needs to be an array that will hold the person obj each new entry to the array is added to the next avail pos in list
public Database(int capacity) {
entry = new Person[capacity];
size = 0;
}
public Person[] getDatabase() {
return entry;
}
Storage Class:
public dataBase writeCommaSeparated(Database data) throws IOException {
Database db = new Database();
PrintStream writer = new PrintStream(file);
if(file.exists()) {
for(int i = 0; i < data.size; i++) {
writer.println(data.get(i).toFile());
}
}
writer.close();
return db;
}
public dataBase read() throws IOException {
Database db = new Database();
Scanner scan = new Scanner(file);
Person person;
//check if file has data print selected data
while(scan.hasNextLine()) {
person = parsePerson(scan.nextLine());
db.add(person);
}
scan.close();
return db;
}
These are just snippets of the code that I have. I am trying to write a sorted array into a file, and I know that it is sorting the file by age correctly but I am not sure how to write it out to a file.
in main I have:
String fileLocation = File.separator + "Users"
+ File.separator + "USERNAME"
+ File.separator + "Desktop"
+ File.separator + "DataFile.txt";
FileStorage fileStore = new FileStorage(fileLocation);
FileData data = fileStore.read(); // this invokes a method called read that reads the file
data.sort(); // sorts the file by age and prints out to the console the sorted age
fileSort.writeCommaSeparated(data); // writes to the file in a commaseparated way
Focusing on just the sorting of a csv file based on age and given your description, this was about the simplest solution that came to mind.
public class PersonDatabase {
private ArrayList<String[]> people = new ArrayList();
// Reads the given input file and loads it into an ArrayList of string arrays.
public PersonDatabase(String inputFile) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(inputFile));
for (String line = null; null != (line=in.readLine()); ) {
people.add(line.split(",")); // convert csv string to an array of strings.
}
in.close();
}
private static final int AGE_COLUMN_INDEX=2; // Identifies the 'age' column
// performs a numeric comparison on the 'age' column values.
int compareAge(String[] a1, String[]a2) {
return Integer.compare(
Integer.parseInt(a1[AGE_COLUMN_INDEX]),
Integer.parseInt(a2[AGE_COLUMN_INDEX]));
}
// Sorts the list of people by age and writes to the given output file.
public void writeSorted(String outputFile) throws IOException {
PrintWriter out = new PrintWriter(new FileWriter(outputFile));
people.stream()
.sorted(this::compareAge) // sort by age
.forEach(a->{
Arrays.stream(a).forEach(s->out.print(s+",")); // print as csv
out.println();
});
out.close();
}
public static void main(String[] args) throws IOException {
PersonDatabase pdb = new PersonDatabase("persondb.in");
pdb.writeSorted("persondb.out");
}
}
Given the following input:
fred,flintstone,43,
barney,rubble,42,
wilma,flintstone,39,
betty,rubble,39,
This program produces the following output:
wilma,flintstone,39,
betty,rubble,39,
barney,rubble,42,
fred,flintstone,43,
It seemed like marshalling these arrays into Person objects just for the sake of sorting was overkill. However, if you wanted to do that, it would be pretty easy to turn an array of field values into a Person object. I'll leave that to you.
So I am pretty much new at this but I have built a library project that has an ArrayList BookList which in return has elements such as String Title, String Author, and int Quantity. I have an add method and a display method and I want to create a method that I am able to save and load the BookList when I I give the appropriate input to do so. Moreover I want to do so, so when I load the BookList I can make changes to the elements in the ArrayList and it's not just reading from the file.
public class Library implements Serializable{...}
Inside this class are my methods of saving and loading which are called in the main as well as the constructors for the ArrayList.
Save
public void save(){
try{
FileOutputStream fileOut = new FileOutputStream("BookList.tmp");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(BookList);
objOut.close();
}catch(Exception ev){}
}//end of save()
Load
public void load(){
try{
FileInputStream fileInput = new FileInputStream("BookList.tmp");
ObjectInputStream objInput = new ObjectInputStream(fileInput);
List<Book> BookList = (List<Book>) objInput.readObject();
objInput.close();
}
catch (Exception ev){}
}//end of load()
Display
public void displayBooks(){
String t;
String a;
int q;
String s;
Book bo = new Book();
for(int i = 0; i<BookList.size(); i++){
bo = BookList.get(i);
t = bo.gettitle();
a = bo.getauthor();
q = bo.getquantity();
System.out.println(i + "." + t + " " + a + " " + q);
}//end of loop
}//end of displayBooks()
But so far through the display method I have I am not able to see the BookList so I don't know if the save method works in the first place as well. So I want to know if the problem lies here or somewhere else.
The problem is in your load(), you read the book list from the ObjectInputStream, but you simply assigned to a local variable. I believe (though poorly named) your BookList is an instance variable.
So your load() should look like
public void load(){
try{
ObjectInputStream objInput = new ObjectInputStream(FileInputStream("BookList.tmp"));
this.books = (List<Book>) objInput.readObject();
objInput.close();
}
catch (Exception ignored){
// please add some handling please!
}
}
Most probable cause of your problem is that BookList in display() method and the one in load() method are different.
In display method, you are referring to BookList that is an instance variable but in load method, you have stored the result of objInput.readObject() method in a local variable with the same name BookList. I think you wanted to store the result in BookList that is an instance variable.
Try changing
List<Book> BookList = (List<Book>) objInput.readObject();
to
BookList = (List<Book>) objInput.readObject();
I'm trying to figure out how to delete a specific line in my Arraylist when a user types in a book ID number. However it seems that it can't find that ID number anywhere in my Arraylist.
private void removeBook()
// Removes a certain book from the Book List.
{
Scanner IDS = setbookID();
int idNum = IDS.nextInt();
if(bookList.contains(idNum)){ //problem
bookList.remove("B"+Integer.valueOf(idNum));
}
}
private Scanner setbookID(){
Scanner bookID = new Scanner(System.in);
System.out.println("Enter your book's ID number. ");
return bookID;}
bookList is an ArrayList that read through a text file and come out as a String. A line in my text file looks like this:
B998 ; Aithiopika ; Heliodorus ; 1829
However if the user types in "998" it doesn't remove this line from the ArrayList. Any ideas on how I can go about doing this? Would an iterator somewhat help?
EDIT: This is how I added the books to the ArrayList in the first place.
private ArrayList<Book> readBooks(String filename) {
ArrayList<Book> lines = new ArrayList<>();
readTextFileB(filename, lines);
return lines;
}
private void readTextFileB(String filename, ArrayList<Book> lines)
// Reads the books.txt file.
{
Scanner s = null;
File infile = new File(filename);
try{
FileInputStream fis = new FileInputStream(infile);
s = new Scanner(fis);
} catch (FileNotFoundException e){
e.printStackTrace();
}
while(s.hasNextLine())
lines.add(new Book(s.nextLine()));
}
You have multiple problems with your code. Not clear what you are trying to achieve whether you want to remove the Book object from the list. If so then you need to compare the ID field(using iterator) of the book object assuming your Book class is like below:
class Book {
int id;
Book(int id) {
this.id = id;
}
}
If above case is not the scenario then your list is a list of Book object then how can you pass String as parameter on the remove method bookList.remove("B"+Integer.valueOf(idNum));
you should pass Book object here or index number.
i am working with a app which is similar to contacts app.Here i want store the data of person in a file.i have little experience with databases.I have no experience with files.So i want to work with files.I have a class
class Person
{
String name;
String mobile_number;
String city;
}
Now when the user enters the data i want to store the data in a file.While retrieving i want to retrieve the data based on name or mobile or city.like i may have list of 5 persons and i want to retrieve the data of the person with particular name.So i want to know the best practices for saving and retrieving the data from a file.
--Thank you
Here is an example:
public class Person implements Serializable {
private static final long serialVersionUID = -3206878715983958638L;
String name;
String mobile_number;
String city;
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person p = new Person();
p.name = "foo";
// Write
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.dat"))){
oos.writeObject(p);
}
// Read
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.dat"))) {
Person person = (Person) ois.readObject();
System.out.println(person.name);
}
}
}
Here is sample code to store retrieved data into the file.
try {
// Assuming the Contact bean list are taken from database and stored in list
List<ContactBean> beanList = database.getContactList();
File file = new File(".../filpath/filename.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (ContactBean contactbean : beanList) {
bw.write("name : " + contactbean.getName() + " , ");
bw.write("mobile Number : " + contactbean.getMobileNo() + " , ");
bw.write("city : " + contactbean.getCity() + " , ");
bw.write("/n");
}
bw.close();
} catch (Exception e) {
e.printStrace();
}
I'm struggling to figure out how to read the data from a file we've been given and use that to create an instance of an object. We are given a txt file of customer data for a store. It is in the following format:
123.64382392 12 1.1234123419
Each line of the file is like this. The first column is Arrival time, the second is number of items, and the third is the time it takes the customer to find one item. There are about 100 customers in this file and I'm not sure how to read from the file to create all the instances necessary.
public static void loadCustomers(){
File file = new File("origCustomerArrivals.txt");
try{
Scanner input = new Scanner(file);
while (input.hasNextLine())
{
double arrivalTime = input.nextDouble();
int numItems = input.nextInt();
double selectionTime= input.nextDouble();
Customer newCustomer = new Customer(arrivalTime, numItems,selectionTime);
input.nextLine();
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("file not opened");
}
}
}
Try this:
public static void loadCustomers(){
File file = new File("origCustomerArrivals.txt");
try{
List<Customer> list = new ArrayList<Customer>();
Scanner input = new Scanner(file);
while (input.hasNextLine())
{
String[] values = scan.nextLine().split("\\s+");
arrivalTime = Double.parseDouble(values[0]);
numItems = Integer.parseInt(values[1]);
selectionTime = Double.parseDouble(values[2]);
Customer newCustomer = new Customer(arrivalTime, numItems,selectionTime);
list.add(newCustomer);
input.nextLine();
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("file not opened");
}
}
}
Could you elaborate on what part of your code isn't working? I tested it myself (printed out the values instead of creating a new Customer object), and it works fine. Except "input.nextLine();" in the while loop is not necessary. It will already jump to the next line, and once you reach the end of your file that will likely cause an error to be thrown.
Also, once you create the object instance, I assume you'll want to save it to a list of the objects. You can do this by creating an ArrayList of object Customer outside the loop:
ArrayList<Customer> Customers = new ArrayList<Customer>();
Then as each instance is created in the loop, add it to this ArrayList:
Customers.add(newCustomer);