-1

I'm receiving the user credentials through a form and I need to be able to insert the object into a database.txt file. Program runs but nothing is being written. User class is Serializable

public static long insert(User user) {
    try{
        //implement insert into file "database.txt"
        FileOutputStream fos = new FileOutputStream("database.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(user);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
 }

User Class: package business;

import java.io.Serializable;

public class User implements Serializable {

private String firstName;
private String lastName;
private String email;
private String password;

public User() {
    firstName = "";
    lastName = "";
    email = "";
    password="";
}

public User(String firstName, String lastName, String email,String password) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.password = password;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
 public String getPassword() {
    return password;
}

public void setPassword(String email) {
    this.password = password;
}

}

  • 2
    You should never use an ObjectOutputStream to write to a text file. This type of stream writes out non-text bytes that represent the data of a Java instance and is thus unreadable by a text reader, and perhaps that's what you're doing (hard to tell). – Hovercraft Full Of Eels Oct 04 '15 at 14:42
  • Is `User` class Serializable? – Rahman Oct 04 '15 at 14:44
  • Yes it is Serializable. – Willyfromnc Oct 04 '15 at 14:54
  • I feel there is something else which is breaking and due to that,this part of code does not get a chance for execution which is why database.txt is coming blank.Check the logs or you can debug to check whether control goes to that part or not. – RockAndRoll Oct 04 '15 at 15:37

2 Answers2

0

Could be a number of issues:

1) You say it is so, but are you really sure the User class is serializeable? Try doing a System.out.println(user).

2) Are you sure the folder the program is writing "database.txt" to is accessible with the write permission for the user java is running for? At the operating system level.

3) Not really an issue, but I'd also try an alternate way to write to the file. Such as through apache ioutils. Check this: Append data into a file using Apache Commons I/O

Best of luck.

Community
  • 1
  • 1
AdeelMufti
  • 341
  • 1
  • 8
0

I tried as suggested in your program and it is working as expected.Sharing the working code.

import java.io.*;

public class ObjectInputStreamExample {

public static class Person implements Serializable {
   
    private static final long serialVersionUID = 1L;
    public String name = null;
    public int    age  =   0;
}


public static void main(String[] args) throws  FileNotFoundException,IOException{

    Person person = new Person();
    person.name = "Prince";
    person.age  = 25;

    insert(person);

}

public static void insert(Person user) throws  FileNotFoundException,IOException{
    try(ObjectOutputStream objectOutputStream =
            new ObjectOutputStream(new FileOutputStream("c:/Sample/database.txt"))){
        objectOutputStream.writeObject(user);
        objectOutputStream.close();
        
    } 
}

} Output: output

The insert method provided in your code wont compile so I have changed return type here from long to void.

More or less it seems that either database.txt is not accessible while writing or there is something else you are trying to do in code and its failing.

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35