I have the following class:
public class Employees implements Serializable{
private final ArrayList<Employee> llistaEmployees = new ArrayList<>();
/**
* Obte la llista d'llistaEmployees que conte la carpeta
* @return ArrayList que conte les llistaEmployees de la carpeta
*/
public List<Employee> getEmployees() {
return llistaEmployees;
}
}
public class Employee implements Serializable{
private String nom;
private String empresa;
private boolean jubilat;
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getEmpresa() {
return empresa;
}
public void setEmpresa(String empresa) {
this.empresa = empresa;
}
public boolean isJubilat() {
return jubilat;
}
public void setJubilat(boolean jubilat) {
this.jubilat = jubilat;
}
public Employee(){
}
public Employee (String nom,String empresa,boolean jubilat){
this.nom=nom;
this.empresa=empresa;
this.jubilat=jubilat;
}
}
And I have to do the following ... Read the file for example Datos.Txt which is where I have the information of the workers and save it in a binary file.
I mean I can read the information from an external file but I have no idea how to do it so that I can save it in another format using these classes ..
Can you help me?
For what I think I understand, it would be.
1- Read the file (I have it)
2- use it to create a list of employees 3- save that list in a binary file.
I'm pretty blocked .. I accept advice .. help .. examples ..: (
For example to read the external file I use this:
public class LecturaFitxer {
/**
* Mètode per llegir les dades d'un fitxer.
* @param filePath d'on llegirem la informació dels employees
* @throws FileNotFoundException excepcio
* @throws IOException excepcio
*/
@SuppressWarnings("ConvertToTryWithResources")
public static void llegeixFitxer(String filePath) throws FileNotFoundException, IOException {
try {
File origen = new File(filePath);
Scanner reader = new Scanner(origen);
String str;
while (reader.hasNextLine()){
str = reader.nextLine();
StringTokenizer st = new StringTokenizer(str);
String[] strList = new String[st.countTokens()];
for(int i=0; i<strList.length; i++){
strList[i] = st.nextToken();
}
System.out.println(strList[0]+"\t\t" +strList[1]+"\t" +strList[2]);
}
reader.close();
} catch(FileNotFoundException ex){
System.out.println("Error llegint el fitxer " + ex);
}
}
public static void main(String args[]) throws IOException{
LecturaFitxer.llegeixFitxer(args[0]);
}
}
Basically what I get is that arg [0] reads me the name, arg [1] the retirement date and the arg [2] the company.
Now what I do not know is that once I have read the file with its data, I use the employees class and I save everything in a bin file ...