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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:21:22+00:00 2026-06-13T23:21:22+00:00

I am trying to get the output of my java program to write to

  • 0

I am trying to get the output of my java program to write to a file.

The user inputs some data which should not be included in the file. When the program responds it should output information to the user, as well as write SOLELY the output to a file.

From examples I have begun with this at the top of my driver class:

static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static String lineFromOutput;

This code is in every place where I might receive output from the program:

try {
    lineFromInput = in.readLine();
    FileWrite.write(lineFromInput);
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

And the class its calling is:

public class FileWrite {
    public static void write(String message) { 
        PrintWriter out = null;
        try {
            out = new PrintWriter(new FileWriter("output.txt"), true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.write(message);
        out.close();
    }
}

It creates the output file, but thats it. None of the output from the program is written.
I’ve looked over numerous examples and this seems to be the easiest way to get the ball rolling, although I’m open to any other suggestions.

Thanks!

  • 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-13T23:21:22+00:00Added an answer on June 13, 2026 at 11:21 pm

    I think it should be InputStremReader with single t in statement below:

    static BufferedReader in= new BufferedReader(new OutputtStreamReader(System.in));
    static String lineFromOutput;
    

    As

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static String lineFromOutput;
    

    EDIT: This works fine. Please make sure you provide the input through input console. Also please note that it reads and write(overwrite) single line ONLY.

        public class FileWrite {
           public static void write(String message) { 
                    PrintWriter out = null;
                  try {
                      out = new PrintWriter(new FileWriter("output.txt"), true);
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                    out.write(message);
                    out.close();
            }
    
           public static void main(String[] args){
               String lineFromInput;
               try {
                    BufferedReader in = new BufferedReader(
                                                new InputStreamReader(System.in));
                    lineFromInput = in.readLine();
                    FileWrite.write(lineFromInput);
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
           }
         }
    

    EDIT 2: Updated program for multi-line inputs. Its not best way to open and close file each time to write, but I am just trying to make your program work with minor changes. Let me know, if you need suggestion to avoid repeated opening/closing of the output file.

    Change Highlights:

    1. Read lines until “exit”(change the word as desired) is received in input
    2. Open the file in append mode.

      public class FileWrite {
         public static void write(String message) { 
                  PrintWriter out = null;
                try {
                    out = new PrintWriter(new FileWriter("output.txt", true), true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  out.write(message);
                  out.close();
          }
      
         public static void main(String[] args){
             String lineFromInput = "";
             try {
                  System.out.println("Provide the inputs in any number of lines");
                  System.out.println("Type \"exit\" in new line when done");
                  BufferedReader in = new BufferedReader(
                                      new InputStreamReader(System.in));
                  while(!"exit".equals(lineFromInput)){
                     lineFromInput = in.readLine();
                     FileWrite.write(lineFromInput+System.lineSeparator());
                  }
                  in.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
             }
         }
       }
      

    EDIT3: Your updated program using Scanner to read the inputs:

            private static HashMap<Integer, Object> shapes = 
                                                  new HashMap<Integer, Object>();
            static int i = 0;
    
            public static void main(String[] args) {
                PrintWriter output = null;
                Scanner scanner = new Scanner(System.in);
                try {
                    output = new PrintWriter(new FileWriter("output.txt"), true);
                } catch (IOException e1) {
                    System.err.println("You don't have accress to this file");
                    System.exit(1);
                }
                String command = "";
                while(!"quit".equalsIgnoreCase(command)){
                    System.out.println("Enter your Command: ");
                    command = scanner.next();
                    if (command.equalsIgnoreCase("create")) {
                        String type = scanner.next();
                        if (type.equalsIgnoreCase("line")) {
                            double length = scanner.nextDouble();
                            Line l = new Line(length);
                            scanner.nextLine();//flush the previous line
                            String line = scanner.nextLine();
                            output.format("%s", line);
                            shapes.put(i, l);
                            i++;
                        }else if (type.equalsIgnoreCase("circle")) {
                            double radius = scanner.nextDouble();
                            String color = scanner.next();
                            Circle c = new Circle(radius, Colors.valueOf(color));
                            scanner.nextLine();//flush the previous line
                            String line = scanner.nextLine();
                            output.format("%s", line);
                            shapes.put(i, c);
                            i++;
                        }else if (type.equals("rectangle")) {
                            double length = scanner.nextDouble();
                            double width = scanner.nextDouble();
                            String color = scanner.next();
                            Rectangle r = new Rectangle(length, width,
                            Colors.valueOf(color));
                            scanner.nextLine();//flush the previous line
                            String line = scanner.nextLine();
                            output.format("%s", line);
                            shapes.put(i, r);
                            i++;
                        }else if (type.equals("square")) {
                            double length = scanner.nextDouble();
                            String color = scanner.next();
                            Square s = new Square(length, Colors.valueOf(color));
                            scanner.nextLine();//flush the previous line
                            String line = scanner.nextLine();
                            output.format("%s", line);
                            shapes.put(i, s);
                            i++;
                        }
                    }else if (command.equals("printbyperimeter")) {
                        Shape[] shapeArray = shapes.values().toArray(new Shape[0]);
                        Arrays.sort(shapeArray);
                                System.out.println("Print in ascending order...");
                        for (int j = 0; j < shapeArray.length; j++) {
                            Shape temp = shapeArray[j];
                            if (temp.getClass().getName().equals("Line")) {
                                System.out.println("Shape: " 
                                        + temp.getClass().getName() + ", Perimeter: "
                                        + temp.getPerimeter());
                                    } else {
                                System.out.println("Shape: " 
                                        + temp.getClass().getName() + ", Color: "
                                        + ((Colorable) temp).getColor()
                                        + ", Perimeter: " + temp.getPerimeter());
                                    }
                                }
                    }else if (command.equals("printbyarea")) {
                        Shape[] shapeArray = shapes.values().toArray(new Shape[0]);
                        System.out.println("Print in random order...");
                        for (int j = 0; j < shapeArray.length; j++) {
                            Shape temp = shapeArray[j];
                            if (!temp.getClass().getName().equals("Line")) {
                                System.out.println("Shape: "
                                        + temp.getClass().getName() + ", Color: "
                                        + ((Colorable) temp).getColor() + ", Area: "
                                        + ((Areable) temp).getArea());
                                    }
                            }
                    }else if (command.equals("quit")) {
                        scanner.close();
                        System.exit(0);
                    }
               }
               output.close();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems trying to get the code below to output the data
I'm trying to get my program to save the output to a file for
I am currently trying to get a script to write output from other started
I'm trying to get my java program to run an svn command from the
I am new to Java and trying to write a program.. here is the
I'm trying to get my java program to talk to a MySQL DB. So
I'm new to Java programming... I'm trying to read data from text file and
I am trying to write a program that uses file I/O to score personality
I'm trying to call a C program from Java and capture the standard output.
made a short java program in Bluej upon compiling it and trying to get

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.