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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:00:16+00:00 2026-05-24T02:00:16+00:00

I’m sure there is a fairly simple answer to this question, so here we

  • 0

I’m sure there is a fairly simple answer to this question, so here we go.

I’m trying to use a FileWriter to write text to a file. My program reads text in from an already existing file, specified by the user and then asks whether to print the text to the console or to a new file, also to be named by the user.

I believe my problem is with passing the FileWriter to the “FileOrConsole” method. Am I not passing or declaring the FileWriter in the “FileOrConsole” method correctly? The file is always created but nothing is written to it.

Here is the code:

import java.io.*;
import java.util.*;

public class Reader {

public static void main(String[] args) throws IOException {
    Scanner s = null, input = new Scanner(System.in);
    BufferedWriter out = null;

    try {
        System.out.println("Would you like to read from a file?");
        String answer = input.nextLine();

        while (answer.startsWith("y")) {
            System.out.println("What file would you like to read from?");
            String file = input.nextLine();
            s = new Scanner(new BufferedReader(new FileReader(file)));

            System.out
                    .println("Would you like to print file output to console or file?");
            FileOrConsole(input.nextLine(), s, input, out);
            System.out
                    .println("\nWould you like to read from the file again?");
            answer = input.nextLine();
        }
        if (!answer.equalsIgnoreCase("yes")) {
            System.out.println("Goodbye!");
        }

    } catch (IOException e) {
        System.out.println("ERROR! File not found!");
        // e.printStackTrace();
    } finally {
        if (s != null) {
            s.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

public static void FileOrConsole(String response, Scanner s, Scanner input,
        BufferedWriter out) {
    if (response.equalsIgnoreCase("console")) {
        while (s.hasNext()) {
            System.out.println(s.nextLine());
        }
    } else if (response.equalsIgnoreCase("file")) {
        System.out.println("Name of output file?");
        response = input.nextLine();
        try {
            out = new BufferedWriter(new FileWriter(response));
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (s.hasNext()) {
            try {
                out.write(s.nextLine());
                out.newLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("Sorry, invalid response. File or console?");
        response = input.nextLine();
        FileOrConsole(response, s, input, out);
    }
  }
}
  • 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-24T02:00:17+00:00Added an answer on May 24, 2026 at 2:00 am

    you make classic error forgetting that parameters passed by value in case of java it is a value of the reference. The thing is that your assignment

    out = new BufferedWriter(new FileWriter(response));
    

    actually does not change the variable declared in main() it stays null

    BufferedWriter out = null;
    and then in finally it skips the close() by the if(out==null)
    and as it is Buffered and you do no flush nothing is written to file.
    what you got to do is out.close(); in side the FileOrConsole method call

    OR

    do the out = new BufferedWriter(new FileWriter(response));
    outside of it. You choose 🙂

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.