Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:02:24+00:00 2026-05-27T20:02:24+00:00

This is for a homework I’m doing on my walk learning java. I’m writing

  • 0

This is for a homework I’m doing on my walk learning java.

I’m writing a program and it is all working as expected except the read/write to file.

I have one class named Medico that holds only one string (typeOfMedico) and one int (valorFacturado). Medico is a sub class of Pessoa. Pessoa holds data like name and address. public class Medico extends Pessoa implements Serializable is the main function on Medicoclass.

On my main class, named Clinica, I ask for user input and at the end of I create a new Medico that its added to an Arraylist named medico.

For reading and writing to file I’ve created this class:

package clinica;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FicheiroObjectos {

    private ObjectInputStream iS;
    private ObjectOutputStream oS;

    public void abreLeitura(String nomeDoFicheiro) throws IOException {
        iS = new ObjectInputStream(new FileInputStream(nomeDoFicheiro));
    }

    public void abreEscrita(String nomeDoFicheiro) throws IOException {
        oS = new ObjectOutputStream(new FileOutputStream(nomeDoFicheiro));
    }

    public Object leObjecto() throws IOException, ClassNotFoundException {
        return iS.readObject();
    }

    public void escreveObjecto(Object o) throws IOException {
        oS.writeObject(o);
    }

    public void fechaLeitura() throws IOException {
        iS.close();
    }

    public void fechaEscrita() throws IOException {
        oS.close();
    }

    public void leFicheiroMedicos() {
        Medico medicos;
        while (true) {
            try {
                medicos = (Medico) this.leObjecto();
                Clinica.medicos.add(medicos);
            } catch (EOFException eof) {
                break;
            } catch (ClassNotFoundException cnf) {
                System.out.print("\nClassNotFoundException!\nO programa vai terminar\n");
                System.exit(-1);
            } catch (IOException ioe) {
                System.out.print("\nErro ao ler o ficheiro!\nO programa vai terminar\n");
                System.exit(-1);
            }
        }
    }

    public void escreveFicheiroMedicos() {
        try {
            for (Medico medicos: Clinica.medicos) {
                this.escreveObjecto(medicos);
            }
        } catch (IOException e) {
            System.out.print("\nErro ao escrever no ficheiro!\nO programa vai terminar\n");
            System.exit(-1);
        }
    }
}

On my main class I’ve created this two functions:

 public static void insereDadosExistentes() {

        try {
            FicheiroObjectos file = new FicheiroObjectos();
            file.abreLeitura("Medicos.dat");
            file.leFicheiroMedicos();
            file.fechaLeitura();
        } catch (IOException ioe) {
        }

    }


    public static void gravarMedicos() {

        try {
            FicheiroObjectos file = new FicheiroObjectos();
            file.abreEscrita("Medicos.dat");
            file.escreveFicheiroMedicos();
            file.fechaEscrita();
        } catch (IOException e) {
            System.out.print("\nErro ao escrever no ficheiro!\nO programa vai terminar\n");
            System.exit(-1);
        }

    }
}

Then added insereDadosExistentes() at the beginning of my mainfunction and added gravarMedicos() just after adding a Medico to my medicos arraylist.

When I run my program (On the first run, file Medicos.dat, does not exist) and create a Medico, Medico is added to my arraylist and the file Medicos.dat is created. Then I stop the program and on the next run, which now haves a Medicos.dat file, I get the error:

Erro ao ler o ficheiro!
O programa vai terminar

The problem is in writing the file or reading the file?
I know the error is given when reading the file but it could be because the writhing to file is not properly executed.

If I try to open Medicos.dat I can see some characters but nothing related with the info I input so I don’t even know if the file writing is ok.

Remember that all besides file handling is working as expected.

Can you point me In some directions?

favolas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:02:25+00:00Added an answer on May 27, 2026 at 8:02 pm
    1. Make sure that you explicitly close the ObjectOutputStream so that all the data is written.

    2. Your problem is an IOException. However, the backtrace will tell you what’s going on: trouble opening, reading, what? you can call printStackTrace(), but better you can use a debugging and just look at the stack trace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with this Java homework question: Write a new method, Refund, that simulates
I'm working on this homework that's kind of confusing me... I am provided with
This is homework. Beginning Java class. Still wrapping my head around this stuff. The
This is not homework, I need this for my program :) I ask this
I need to write a function that can read a file, and add all
I have this homework. And I am beginner C# program, and now learn the
FYI This is homework. I have to build a Java Chat server. I have
I've done this function in C using system calls (open, read and write) to
I am doing this homework task where it requires me to take in a
DISCLOSURE: This is homework. The code below is meant to read a command file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.