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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:34:47+00:00 2026-06-03T09:34:47+00:00

I just started doing file I/O andim using an example from Murach’s Se 6.

  • 0

I just started doing file I/O andim using an example from Murach’s Se 6.
Here is my code. Am i missing something. I know the code further on has more but as this is an example this should work right?

//Import import java.io.*; for use with the File I/O Methods.
import java.io.*;
public class MainApp 
{

    public static void main(String[] args) 
    {
    //Create a file object.
    File productFile = new File("product.txt");
    //Open a buffered output stream to allow write to file operations.
    PrintWriter out = new PrintWriter(
                      new BufferedWriter(
                      new FileWriter(productFile)));      

    out.println("java\tMurach's Beginning Java 2\t$49.99");
    out.close();

    BufferedReader in = new BufferedReader(
                        new FileReader(productFile));

    String line = in.readLine();
    System.out.println(line);

    out.close();


    }

}

//Answer

by adding a throws exception to the end of where i initialised the main this code works. Even the txt file products.txt is in the class folder as expected.
//Import import java.io.*; for use with the File I/O Methods.

import java.io.*;
public class MainApp 
{

    public static void main(String[] args) throws Exception 
    {
    //Create a file object.
    File productFile = new File("product.txt");
    //Open a buffered output stream to allow write to file operations.
    PrintWriter out = new PrintWriter(
                      new BufferedWriter(
                      new FileWriter(productFile)));      

    out.println("java\tMurach's Beginning Java 2\t$49.99");
    out.close();

    BufferedReader in = new BufferedReader(
                        new FileReader(productFile));

    String line = in.readLine();
    System.out.println(line);

    out.close();


    }

}
  • 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-03T09:34:49+00:00Added an answer on June 3, 2026 at 9:34 am

    The problem is that a number of the calls to the java.io package throw exceptions.

    easy fix: add the following to your method signature

     public static void main(String[] args) throws IOException 
    

    almost as easy fix: add try/catch/finally blocks.

    public static void main(String[] args) 
    {
        //Create a file object.
        File productFile = new File("product.txt");
    
        //Open a buffered output stream to allow write to file operations.
        PrintWriter out = null;
        try {   
            out = new PrintWriter(
                    new BufferedWriter(
                            new FileWriter(productFile)));      
    
            out.println("java\tMurach's Beginning Java 2\t$49.99");
        }
        catch(IOException ex) {
            // todo exception handling
    
            System.out.println("ERROR! " + ex);
    
        }
        finally {
            out.close();
    
        }
    
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new FileReader(productFile));
    
            String line = in.readLine();
            System.out.println(line);
        }
        catch (IOException ex) {
            // todo more exception handling
            System.out.println("ERROR! " + ex);
        }
        finally {
            in.close();
        }
    
    }
    

    edit: you know you are trying to call out.close() twice? The second should be a call to in.close()

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

Sidebar

Related Questions

I'm sure I'm missing something pretty basic, but I have just started to get
I've just started using Backbone (and Underscore) JS. We are doing a big iPad
I've just started using Jenkins today, so it's entirely possible that I've missed something
I just started using R and like to know how to plot a line
I've just started doing some real-world performance testing on my Fluent NHibernate / SQLite
I just started working with play, and I modified the way I'm doing a
I'm doing a .NET unit as part of my studies. I've only just started,
Just started with JQuery and I've managed to pass a parameter using POST, firebug
Just started coding in AS3 with FlashDevelop and coming from a C# background, I
Just started working with Mercurial a few days ago and there's something I don't

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.