coc champ
coc champ

Reputation: 69

Why fileWriter is not saving the content to my file?

It's not showing any error but the content should be saved to my file, which is not saving...

import java.util.Scanner;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileReadLine {

    public static void main(String[] args) {

        try {

            String str;
            Scanner sc=new Scanner(System.in);

            do {

              System.out.println("Enter your lines");
              str=sc.nextLine();
              FileWriter fw = new FileWriter("C:/test/abcd.txt");

              if(!str.equals("stop"))
                fw.write(str);
              fw.write("\n");
              fw.close();
            } while(!str.equals("stop")); 

        } catch (Exception ex) {
            System.out.println(ex);
        }
     }
  }

please correct my code if i am wrong

Upvotes: 1

Views: 2082

Answers (3)

jonhid
jonhid

Reputation: 2135

You were closing the writer in every iteration since you are not using braces in the if condition...

Try this solution, is working

    try {

        String str;
        Scanner sc=new Scanner(System.in);

        File fw = new File("C:/Users/MYPC/Desktop/abcd.txt");
        FileOutputStream fos = new FileOutputStream(fw);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));


        do {

          System.out.println("Enter your lines");
          str=sc.nextLine();

         if(!str.equals("stop")) {
              bw.write(str);
              bw.newLine();
          } else {
              bw.close();
          }

        } while(!str.equals("stop")); 

    } catch (Exception ex) {
        System.out.println(ex);
    }

Upvotes: 1

Alpesh Jikadra
Alpesh Jikadra

Reputation: 1722

You must close you FileWriter (fw) out of while loop.

Try below code

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

    FileWriter fw = null;
    try {
        fw = new FileWriter("C:/Users/MYPC/Desktop/abcd.txt");
        String str;
        Scanner sc = new Scanner(System.in);

        do {
            System.out.println("Enter your lines");
            str = sc.nextLine();

            if (!str.equals("stop")){
                fw.write(str);
            }
            fw.write("\n");

        } while (!str.equals("stop"));

    } catch (Exception ex) {
        System.out.println(ex);
        // Logger.getLogger(FileReadLine.class.getName()).log(Level.SEVERE,
        // null, ex);
    }finally{
        if(fw != null){
            fw.close();
        }
    }
}

Upvotes: 1

Srinath S
Srinath S

Reputation: 23

You are trying to create a new file inside the loop. So it gets overridden. Change the program to create the file once(before loop) and use it inside the loop to write it.

Also do not close the file as soon as you have written it. Use it once you encounter "stop". Close() should be used when you are done with writing into the file.

Try using flush() before close() to send all data in the buffer to the the file.

Upvotes: 2

Related Questions