2

I have to take name and address of user from user and put it into textfile. I write following code:

package selfTest.nameAndAddress;

import com.intellij.codeInsight.template.postfix.templates.SoutPostfixTemplate;

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

/**
 * Created by 
 */
public class Test {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);


        //creating addressbook text file
        File fl=new File("E:/addressbook.txt");
        fl.createNewFile();

        FileReader fr=new FileReader(fl);
        BufferedReader in=new BufferedReader(fr);



        boolean eof=false;
        int inChar=0;

        String[] name=new String[2];
        String[] address=new String[2];

        int counter=0;


        do{
            FileWriter fw=new FileWriter(fl);
            BufferedWriter out=new BufferedWriter(fw);

            System.out.println("Enter "+(counter+1)+" students name "+" and address");

            name[counter]=br.readLine();
            address[counter]=br.readLine();

            out.write(name[counter]);
            System.out.println("Nmae: "+name[counter]+" ddress: "+address[counter]);
            counter++;
        }while(counter<2);
    }
}

When I run the code, it takes input from user but the text file is empty. The address and name is not written into text file. How can I store the name and address into text file in the above code.

Jeeter
  • 5,887
  • 6
  • 44
  • 67
Himal Acharya
  • 836
  • 4
  • 14
  • 24
  • You need to close the stream. – Rabbit Guy Aug 15 '16 at 18:34
  • 4
    I would recommend using [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) blocks for your streams, readers, writers, etc. to ensure everything is closed and flushed appropriately. – ManoDestra Aug 15 '16 at 18:35

2 Answers2

6

You create the BufferedWriter, but never flush or close it.

These operations are what actually write to the file


As @ManoDestra pointed out in the comments, Java supports the try-with-resources statement, which allows you to format your statements like:

try(BufferedWriter out = new BufferedWriter(new FileWriter(fl))) {

Since BufferedWriter implements the AutoCloseable interface, Java will automatically take care of cleanup of out when the try block exits

Jeeter
  • 5,887
  • 6
  • 44
  • 67
  • 1
    @ManoDestra I never actually knew about the try-with-resources. Thanks for teaching me something new today! :) – Jeeter Aug 15 '16 at 18:40
  • 1
    You're welcome. A lot of good stuff in Java 7 and 8 now. I use try-with-resources a lot now and custom functional interfaces to allow me to pass lambdas that throw exceptions. Check those out too :) – ManoDestra Aug 15 '16 at 20:28
1

A simpler alternative to BufferedWriter is PrintStream:

PrintStream printer = new PrintStream(new File("filepath"));
System.setOut(printer);

And then you can print whatever you want to the file, e.g.

printer.println(name[counter]);

And then close it at the end:

printer.close();
AlterV
  • 301
  • 1
  • 3
  • 10
  • I don't know if this is really a solution. OP is wondering why the file isn't getting written to, and you're proposing changing his code completely without saying why – Jeeter Aug 15 '16 at 18:42
  • 1
    @Jeeter Oh, sorry I'm new here. I saw your response and I just wanted to suggest an alternative for future reference. – AlterV Aug 15 '16 at 18:45
  • How have I never seen `System.setOut`... Why didn't you use `System.out` after that? – 4castle Aug 15 '16 at 18:46
  • @4castle You can use `System.out.print` or `printer.print` after `System.setOut`. I just redundantly put it both ways in case I forget and use `System.out` when I shouldn't be. – AlterV Aug 15 '16 at 18:50