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

  • Home
  • SEARCH
  • 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 6949233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:55:38+00:00 2026-05-27T13:55:38+00:00

I have to read a text and I created a method public void load(String

  • 0

I have to read a text and I created a method

public void load(String fname){

    try{

        BufferedReader reader = new BufferedReader(new FileReader(fname));

        String id_cliente = reader.readLine();
        while(id_cliente!=null){

            String name_surname = reader.readLine();
            int num_titoli = Integer.parseInt(reader.readLine());

            String[] sb = name_surname.split(" ");

            Cliente cl = new Cliente(id_cliente,sb[0],sb[1]);

            clientilist.put(Integer.parseInt(id_cliente.substring(1)),cl);

            for(int i = 0; i < num_titoli; i++){

                cl.addTitolo(String titolo = reader.readLine());
            }

            id_cliente = reader.readLine();
        }
    }
    catch(FileNotFoundException fnfe){

            try{


            }
            catch(FileNotFoundExeption fnfe){

                System.exit(0);
            }
    }
    catch(IOException ioe){

    }
}

what I would do is to check if the fname file exists.if it’s not a FileNotFoundExceptionwill be thrown.Inside it I have to try to open another file.if it is not present so exit with an error message.how can i do?

  • 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-27T13:55:38+00:00Added an answer on May 27, 2026 at 1:55 pm

    In the catch block of the first try catch statement you could put any code you want and it will be executed when the exception occurs. You could read another file, try reading the same file again, ask user to point to the correct file, …

    But as mentioned a better solution is to check if the file exists before you create the reader. And if that fails you can fallback on another file (what if that one fails also?)

    In the next code I adapted your method to have a check and throw an exception if file isn’t valid. On using that method you can react on that. Note that you haven’t opened any readers if you gave 2 invalid filenames.

    try{
       load(fname);
    }catch(Exception e){
       try{
          load(alternativeFName);
       }catch(Exception e){
          System.out.println("None of the files are available");
          e.printStackTrace();
       }
    }
    

    And this is how your load function would look like:

    public void load(String fname) throws Exception {
       // try opening file    
       File file = new File(fname);
       // check if valid file
       if( !file.exists() ){
          // if no valid file throw exception so we can react on that
          throw new Exception("File not available: "+fname);
       }
       //your code for reading here, at this point you know the file exists
       //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page with a read-only text box which shows some HTML
I have successfully managed to get System.Speech.Synthesis to read English text in arbitrary voices
I'm trying to have my application read through a text file and look for
I have a text file and i want to to read it line by
I have a small text file that I'd like to read into a scalar
I have a text file. I want read that file. But In that if
I have this API function: public ResultEnum DoSomeAction(string a, string b, DateTime c, OtherEnum
I have read through several reviews on Amazon and some books seem outdated. I
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read about partial methods in the latest C# language specification , so

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.