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 8746949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:16:36+00:00 2026-06-13T12:16:36+00:00

I am writing data to a file, when I write this data I want

  • 0

I am writing data to a file, when I write this data I want to do it so that if the file does not open it will give the user a message saying that something whent wrong. The way I do this is by calling the method to write, if it fails it returns false. That way I can prompt the user to do something to check what has happened.

However when I create the object I cant return anything from the constructor so I am a bit stumped about what I should do.

public class Writetofile {
BufferedWriter writer = null;

public Writetofile(String[]details) throws IOException{
String machine= details[0];
String date=details[1];
String start_time = details[2];     
try{
   File new_cal= new File("C:\\Activity_Calibrator\\log\\"+machine+"\\"+machine+date+".txt");
   new_cal.getParentFile().mkdir();
   FileWriter fwriter = new FileWriter(new_cal);
   writer = new BufferedWriter(fwriter); 
   writer.write("Linear Calibratiton for " + machine + " carried out " + date+" ./n");
   writer.close();
  }
catch(Exception e){ in here I would like to be able to send a message back to m
code so that it can tell the user to check the folder etc}
} 

when I call the record data if something goes wrong it will return a false to the calling class. and I can put a message.

 public boolean recordData(String record) throws IOException{
try{
    writer.append(record);
    writer.close();
    return true;
   }
catch(Exception e){
    return false;

   }
 }
 }
 } 
  • 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-06-13T12:16:37+00:00Added an answer on June 13, 2026 at 12:16 pm

    A constructor should not DO anything. A constructor is an initialization phase closely tied to the allocation of an object.

    Throwing exceptions, or doing anything in a constructor that might throw an exception is to be avoided.

    Java does not separate the phases of allocation and initialization, no code, especially IO code should be in a constructor.

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

Sidebar

Related Questions

I am writing data in a file. The file will look like this. [section1]
I want to save space when writing my data to file. That is I
Whats the best approach for writing data to a file that will be picked
How can I write data to stdout instead of writing file? I'm using GPG
I'm writing a program that passes data from a file into an array, but
I'm writing a bash script that encrypts the data of a folder or file
I am writing a Silverlight application that will be both reading and writing data
I'm trying to write a sizable chunk of data to a file that is
I'm writing a program that needs to read and write lots of data in
I'm having trouble reading and writing QByteArray data to a file. My goal is

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.