Visualizzazione dei risultati da 1 a 4 su 4

Visualizzazione discussione

  1. #3
    mi potete dire perche non melo stampa sulla console gli oggetti che li carico da file primo_file.dat, alla fine del programma mi stampa : error nel spertura di file
    // classe people
    package person;

    imprt java.io.Serializable;
    public class people implements Serializable{
    protected String name;
    protected String surname;
    protected int age;
    protected String address;

    public people(String nome, String cognome, int eta){
    this.name = new String(nome);
    this.surname = new String(cognome);
    this.age = eta;
    }

    public void adress(String indirizzo){
    this.address = new String(indirizzo);
    }
    public void stampaPeople(){
    System.out.println("nome: " + this.name + " cognome: " + this.surname + " la sua eta: " + this.age + " il suo indirizzo: " + this.address);
    }
    public String toString(){
    return this.name + this.surname + this.age + this.address;
    }
    }

    // classe student
    package person;
    import java.io.Serializable;
    public class student extends people implements Serializable{
    protected int matricola;
    protected String facolta;
    protected String corso;
    protected int year;



    public student (String nome, String cognome, int eta, int mat){
    super(nome,cognome,eta);
    this.matricola= mat;

    }

    public void infoCorso(String fac, String cor, int anno){
    this.facolta = new String(fac);
    this.corso = new String(cor);
    this.year = anno;
    }

    public void stampaStudent(){
    super.stampaPeople();
    System.out.println( "la matricola del studente : "+ this.matricola + "\n fa il corso: "+
    this.corso + " della sua facolta: "+ this.facolta + " \nl'anno del corso: "+ this.year );

    }

    public String toString(){
    return super.toString() + this.matricola + this.facolta + this.corso + this.year;
    }

    }
    //classe readfile
    package person;
    import java.io.*;
    import java.util.ArrayList;
    public class readFile {


    protected File f;

    public readFile(String name){
    this.f = new File(name);

    }

    public ArrayList <student> letturaFile(){
    ArrayList <student> stu = new ArrayList<student>();

    try{
    student s;
    FileInputStream fis = new FileInputStream(this.f);
    ObjectInputStream ois = new ObjectInputStream(fis);


    while((s=(student)ois.readObject()) !=null){
    s.stampaStudent();
    stu.add(s);
    }
    ois.close();
    fis.close();
    }
    catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch(IOException e){
    System.out.println("errore nel apertura di file");
    }
    return stu;
    }
    }

    // clase writefile
    package person;
    import java.io.*;
    import java.util.ArrayList;
    public class writeFile {

    File f;

    public writeFile(String name){
    this.f = new File(name);
    }

    public void insertOutput(ArrayList<student> stu){
    try{
    FileOutputStream fs = new FileOutputStream(this.f);
    //DataOutputStream dos = new DataOutputStream(fs);
    ObjectOutputStream oos = new ObjectOutputStream(fs);
    for (student temp : stu){
    oos.writeObject(temp);
    }

    oos.close();
    fs.close();
    }
    catch(IOException e){
    System.out.println("errore di scrittura nel file:"+ this.f.getName());
    }

    }
    public void insertOutput(student s){
    try{
    FileOutputStream fs = new FileOutputStream(this.f);
    //DataOutputStream dos = new DataOutputStream(fs);
    ObjectOutputStream oos = new ObjectOutputStream(fs);
    oos.writeObject(s);
    oos.close();
    fs.close();
    }
    catch(IOException e){
    System.out.println("errore di scrittura nel file:"+ this.f.getName());
    }

    }

    }

    //classe main
    package person;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class mainClass {


    public static void main(String args[]){

    /* Scanner scan = new Scanner(System.in);
    ArrayList <student> stu = new ArrayList <student>();
    for ( int i =0; i < 3; i++){
    String tempNome, tempCognome;
    int tempEta, tempMat;
    System.out.println("inseriisci nel ordine nome cognome eta mat: ");
    tempNome =scan.nextLine();
    tempCognome = scan.nextLine();
    tempEta= scan.nextInt();
    tempMat = scan.nextInt();
    student temp = new student(tempNome, tempCognome, tempEta, tempMat);

    System.out.println("inserisic informazione del corso nel ordine: facolta, corso ,anno: ");
    String tempFac =new String();
    String tempCor = new String();
    tempFac = scan.nextLine();
    scan.nextLine();
    tempCor = scan.nextLine();
    int tempAnno;
    tempAnno = scan.nextInt();
    scan.nextLine();
    temp.infoCorso(tempFac, tempCor, tempAnno);

    System.out.println("inserisci indirizzo del studente : ");
    String tempAdre;
    tempAdre = scan.nextLine();
    temp.adress(tempAdre);
    stu.add(temp);
    }

    for (student item : stu){
    item.stampaStudent();
    }
    writeFile wf = new writeFile("/home/neptune/Scrivania/primo_file.dat");
    wf.insertOutput(stu);
    */
    ArrayList <student> stu = new ArrayList<student>();
    readFile rf = new readFile("/home/neptune/Scrivania/primo_file.dat");
    stu=rf.letturaFile();
    for (student item : stu){
    item.stampaStudent();
    }

    }
    }
    Ultima modifica di superPoseidon; 27-09-2014 a 18:21 Motivo: funziona tutto perfettamente

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.