Hi!

Could someone please help me to solve one more problem.

To read the data (ArrayList) from file, I'm using the following code, which assumes that columns are separated with ",". It works perfectly:

public static List<UserDataRow> readData()
    {
            List<UserDataRow> listofusers = new ArrayList<UserDataRow>();
            FileInputStream fstream = null;
            try
            {
                fstream = new FileInputStream("src/filetree/UserDataFile.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine = "";
                String[] tokens = strLine.split(", ");
                //Read file line by line
                while ((strLine = br.readLine()) != null)   {
                  // Copy the content into the array
                  tokens = strLine.split(", ");
                  listofusers.add(new UserDataRow(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]));
                }
            }
            catch (IOException e) {
              e.printStackTrace();
            }
            finally {
                try { fstream.close(); } catch ( Exception ignore ) {}
            }
            return listofusers;
    }

To write the date, I've tried using ObjectOutputStream, which actually assumes different separators. It also works correctly:

public void insertrow(String regNr, String firstName, String lastName, String profession, String sex, String dateofbirth) {  
         List<UserDataRow> userDataList = filetree.FilterClass.readuserdatafromfile();  
         UserDataRow ud = new UserDataRow(regNr, firstName, lastName, profession, sex, dateofbirth);  
         userDataList.add(ud);  
         try{  
             FileOutputStream fileOut = new FileOutputStream("src/filetree/UserDataFile.txt");  
             ObjectOutputStream oos = new ObjectOutputStream (fileOut);  
             oos.writeObject (userDataList);  
         }catch(Exception e){  
             System.err.println(e.getMessage());  
         }  
      }

But the problem is these two codes could not be used for the same file, because they assume different data formats. To solve the problem, I've tried using ObjectInputStream for reading the data from file. But I've noticed that the System.out.println(listofusers.size()) = 0. Could someone please explain me why does it happen? What coudl be the best solution for this case? Thanks a lot.

public static List<UserDataRow> readData() {  
     List<UserDataRow> listofusers = new ArrayList<UserDataRow>();     
         FileInputStream fis = null;  
         ObjectInputStream in = null;  
         try {  
             fis = new FileInputStream("src/filetree/UserDataFile.txt");  
             in = new ObjectInputStream(fis);  
             listofusers = (ArrayList) in.readObject();  
             in.close();  
         } catch (IOException ex) {  
             ex.printStackTrace();  
         } catch (ClassNotFoundException ex) {  
             ex.printStackTrace();  
         }  
 System.out.println(listofusers.size());  
                 return listofusers;  
     }

Edited 6 Years Ago by LianaN: n/a

Well just a guess, is your 'UserDataFile.txt' completely clean before you write your ArrayList to it ?
Also try closing your ObjectOutputStream once you are done writing the ArrayList, but since you mention that the object writing code works correctly, that shouldn't be the problem.

Hi!

Thanks!.

well, I've solved my problem, it was a bit stupid and required just more attention to the theory. Now the code works perfectly. If anybody will need the code, then just write me and I will share it here.

Well just a guess, is your 'UserDataFile.txt' completely clean before you write your ArrayList to it ?
Also try closing your ObjectOutputStream once you are done writing the ArrayList, but since you mention that the object writing code works correctly, that shouldn't be the problem.

Hello LianaN!
I have the same problem as you in this thread!
I need your help and please leave the code here or send me via email: <<Email Snipped>> (if you use Yahoo Messenger)
thanks a lot!

Edited 6 Years Ago by __avd: Email Snipped.

Hi!

Below you could find the code. Hope that helped.

public static List<UserDataRow> readDataFromFile()
    {
            List<UserDataRow> listofusers = new ArrayList<UserDataRow>();
            FileInputStream fstream = null;
            try
            {

                fstream = new FileInputStream("src/files/UserDataFile.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine = "";
                String[] tokens = strLine.split(", ");
                //Read file line by line
                while ((strLine = br.readLine()) != null)   {
                  // Copy the content into the array
                  tokens = strLine.split(", ");
                  listofusers.add(new UserDataRow(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]));
                }
            }
            catch (IOException e) {
              e.printStackTrace();
            }
            finally {
                try { fstream.close(); } catch ( Exception ignore ) {}
            }
            return listofusers;
    }


    public void insertUserDataRow(String regNr, String firstName, String lastName, String profession, String sex, String dateofbirth) {
        List<UserDataRow> userDataList = readDataFromFile();
        UserDataRow ud = new UserDataRow(regNr, firstName, lastName, profession, sex, dateofbirth);
        // Add row to ArrayList
        userDataList.add(ud);
        // Add row to file
        tableModel.addRow(ud);
        // Update file
        updateFile("src/files/UserDataFile.txt", userDataList);
    }


    public static void updateFile(String filename, List<UserDataRow> userDataList) {
       BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(filename));
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
       UserDataRow ud;
       String row;
       for(int i=0; i<userDataList.size(); i++) {
           ud = userDataList.get(i);
           row = ud.regnumber + ", " + ud.firstname + ", " + ud.lastname + ", " + ud.profession + ", " + ud.sex + ", " + ud.dateofbirth;
            try {
                bufferedWriter.write(row);
                bufferedWriter.newLine();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        //Close the BufferedWriter
        try {
              if (bufferedWriter != null) {
                  bufferedWriter.flush();
                  bufferedWriter.close();
              }
        } catch (IOException ex) {
              ex.printStackTrace();
        }
    }

//========================================================
public class UserDataRow implements java.io.Serializable {
    public String regnumber;
    public String firstname;
    public String lastname;
    public String profession;
    public String sex;
    public String dateofbirth;
    public int size;

    public UserDataRow(String rn, String fn, String ln, String p, String s, String dob) {
        this.regnumber = rn;
        this.firstname = fn;
        this.lastname = ln;
        this.profession = p;
        this.sex = s;
        this.dateofbirth = dob;
        this.size = 6;
    }

    public String getRegnumber()
    {
        return this.regnumber;
    }

    public String getFirstname()
    {
        return this.firstname;
    }

    public String getLastname()
    {
        return this.lastname;
    }

    public String getProfession()
    {
        return this.profession;
    }

    public String getSex()
    {
        return this.sex;
    }

    public String getDateofbirth()
    {
        return this.dateofbirth;
    }
}
This article has been dead for over six months. Start a new discussion instead.