Aakash Goyal
Aakash Goyal

Reputation: 305

Entered Text is not completely written in file when using Buffered Writer

Below is the code I wrote using BufferedWriter to write a text to a file:

        public class InputOutputStream {

          public static void main(String[] args) 
         {                
         FileReader f = null;
         File fu = new File("C:\\Akki.txt");    
         BufferedReader f1 = null;
         FileWriter f2 = null;
         BufferedWriter f3 = null;

            try{
             String s1;
             f = new FileReader("C:\\Test.txt");
             f1 = new BufferedReader(f);
             while((s1 = f1.readLine())!=null)
             {
            System.out.println(s1);
             }

     //User enters the content and same is written to file

         f2 = new FileWriter(fu, true);
         f3 = new BufferedWriter(f2);

      System.out.println("Enter the text to write in File");

        Scanner sc = new Scanner(System.in);
        String k = sc.next();
        f3.write(k);

        f3.flush();
        f3.close();     
       }
    catch(IOException e)
    {
        System.out.println("IOException");
    }
    finally
    {
        if(f1!= null)
        try{                
            f1.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
      }

   }

}

Now, When I enter text like: " Test data Written Succesfully" only 'Test' is being written and rest of the string is not written

Please help

Upvotes: 1

Views: 104

Answers (1)

smb
smb

Reputation: 834

It's not because of writers. It's because of Scanner. You use next, try nextLine

Upvotes: 3

Related Questions