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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:13:08+00:00 2026-06-02T08:13:08+00:00

I created a file named ‘test.txt’ and then took input from the user to

  • 0

I created a file named ‘test.txt’ and then took input from the user to write the input to the file. Everything runs fine. The program doesn’t show any error at all. The file is created and the program takes input from the user but when I checked the content of the file, it was empty. Can anyone figure out what is wrong with my code? The code is as follows.

package InputOutput;

import java.io.*;

public class CharacterFileReaderAndFileWriter{

private BufferedReader br = null;
private BufferedWriter bw = null;
private PrintWriter pw = new PrintWriter(System.out, true);

public File createFile() throws IOException{
    File f = new File("test.txt");
    return f;
}

public void writeToFile() throws IOException{
    try{
        bw = new BufferedWriter(new FileWriter(createFile()));
    }
    catch(FileNotFoundException ex){
        ex.printStackTrace();
    }

    //take input from the console (user)
    br = new BufferedReader(new InputStreamReader(System.in));
    String s;

    pw.println("Please enter something");
    pw.println("To stop the program, enter 'stop'");

    do{
        s = br.readLine();
        if(s.compareTo("stop")==0)
            break;
        s+= "\r\n";//adding an new line to the string s         
        bw.write(s);            
    }
    while(s.compareTo("stop")!=0);

    br.close();
    bw.close();

}

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

    CharacterFileReaderAndFileWriter cfr = new CharacterFileReaderAndFileWriter();
    cfr.writeToFile();
}

}

  • 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-02T08:13:09+00:00Added an answer on June 2, 2026 at 8:13 am

    Most example programs show that you have to call.flush() on your BufferedWriter before the .close(). This should not be required, .close() should call .flush() automatically, but it doesn’t hurt. Also you should call all the Stream/Writer objects .close() methods in reverse order as well, again correctly written classes should call .close() on all the object they wrap, but it doesn’t hurt to do it anyway.

    Other things that might catch you out later:

    if(s.compareTo("stop")==0)
    

    should be

    if ("stop".equalsIgnoreCase(s))
    

    it is more efficient, eliminates the possibility of a NullPointerException on s, handles any case of stop and most importantly more idiomatic Java than using .compareTo()

        s+= "\r\n";//adding an new line to the string s         
        bw.write(s);        
    

    should be

        bw.write(System.getProperty("line.separator"));        
        bw.write(s);        
    

    The s+= creates intermediate objects and garbage that needs to be collected. Hard coding line endings is bad as well.

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

Sidebar

Related Questions

I have created a file named MyFile.db using SQLite3 from my C#.net application. This
I want to write an XML file. I have created an XSD file named
I know what works If I create a new file in explorer named test.as
I have an XML document as follows: <directory> <file><monitored>0</monitored> <xferStatus>1</xferStatus> <name>test1.txt</name> <size>7</size> <created>03/31/10 11:30:02
I'm trying to follow the instructions here: http://www.php.net/manual/en/features.commandline.usage.php I created a file named vardump
I created one file named br_states.rb in lib/ in the my rails 3 project,
Alright, so I created a JavaScript file named HelloWorld.js with the following contents: java.lang.System.out.println(Hello
I have a appwidget extends from appwidgetprovider. I have created a layout xml file
I have created a file named ab.sql using the mysqldump utility of a database
I have created a templatetags folder inside my application and inside a file named

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.