I am a beginner in java and practicing IO stream and Serialization .In below problem unable to read all data into a Arraylist and print it. by using the ObjectOutputStream insert the content by user in file but unable to get all data. just first part of data are read.
package com.filehandling;
import java.io.*;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name ;
private int age;
private String role;
private int number;
User(){}
public User(String name1, int age1, String role1, int number1){
this.name = name1;
this.age = age1;
this.role = role1;
this.number = number1;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return this.role;
}
public void setRole(String role) {
this.role = role;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return new StringBuffer(" Name : ").append(this.name)
.append(", role : ").append(this.role).append(", Age : ").append(this.age).append(" Number : ").append(this.number).toString();
}
}
package com.filehandling;
import java.io.*;
import java.util.*;
public class Menu {
String name, role;
int age,number;
//User user1 = new User();
File f = new File("students.txt");
public static void main(String[] args) throws IOException {
Menu main = new Menu();
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("Enter 1 for Add data");
System.out.println("Enter 2 for Printing all data");
System.out.println("Enter 3 to Exit");
scan.hasNextLine();
int choice = scan.nextInt();
scan.hasNextLine();
//String extra = scan.nextLine();
switch(choice)
{
case 1:
main.callWriteFile();
break;
case 2:
main.callReadFile();
break;
case 3:
System.exit(1);
break;
default:
System.out.println("\nInvalid Choice !");
break;
}
}
}
public void callWriteFile() {
try {
Scanner scan = new Scanner(System.in);
Menu main = new Menu();
System.out.println("enter name, role, age, number");
name = scan.nextLine();
role = scan.nextLine();
age = scan.nextInt();
number = scan.nextInt();
User user = new User(name,age,role,number);
main.writeData(user);
}
catch(Exception e) {}
}
public void callReadFile() throws IOException {
Menu main = new Menu();
ArrayList<Object> objectsList = new ArrayList<>();
main.readFile(objectsList);
for(Object user1: objectsList) {
System.out.println(user1);
}
System.out.println(objectsList);
}
public ArrayList<Object> readFile(ArrayList<Object> list) throws IOException{
FileInputStream file = new FileInputStream("students.txt");
ObjectInputStream input = new ObjectInputStream(file);
// ArrayList<Object> objectsList = new ArrayList<>();
boolean counter = true;
while (counter) {
try {
Object object = input.readObject();
if (object != null) {
User userTest = (User) object;
if( userTest !=null){
System.out.println(" my "+userTest);
}
list.add(object);
}
input.close();
counter = false;
}
catch(Exception e) {
System.out.println(e);
e.printStackTrace();
return null;
}
}
// input.close();
return list;
}
public void writeData(Object obj) {
try{
FileOutputStream file = new FileOutputStream("students.txt", true);
if (f.length() == 0) {
ObjectOutputStream object = new ObjectOutputStream(file);
object.writeObject(obj);
object.close();
}
else {
MyObjectOutputStream oos = null;
oos = new MyObjectOutputStream(file);
oos.writeObject(obj);
// Closing the FileOutputStream object
// to release memory resources
oos.close();
}
}
catch(Exception e) {
System.out.println(e);
}
}
}
package com.filehandling;
import java.io.*;
import java.util.*;
public class MyObjectOutputStream extends ObjectOutputStream {
// Constructor of this class
// 1. Default
MyObjectOutputStream() throws IOException
{
// Super keyword refers to parent class instance
super();
}
// Constructor of this class
// 1. Parameterized constructor
MyObjectOutputStream(OutputStream o) throws IOException
{
super(o);
}
// Method of this class
public void writeStreamHeader() throws IOException
{
return;
}
}
i tried every possible thing at my level but cannot spot the loophole.
ObjectOutputStreamfor writing plain text. Consider usingjava.nio.file.Files.writeStringor similar methods in that class, which will greatly simplify your code.writeDatamethod is doing something very funky depending on the size of the file. Have you verified that the file contents are as expected after writing, before trying to read them again? Obviously that would be easier when writing a plain String instead of an Object.readFileyou are callinginput.close();within your while loop, while it is commented out after the loop. That closes the input and stops reading once the first object has been read.