Results 1 to 4 of 4

Thread: How to write to file java using bufferedwriter?

  1. #1
    Join Date
    May 2008
    Posts
    33

    How to write to file java using bufferedwriter?

    Hi,

    hey I have just started with Java & want some help.
    How to write to file java using bufferedwriter?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2008
    Posts
    116

    Re: How to write to file java using bufferedwriter?

    Writing to a File
    File gets created if its not existing.

    Code:
    try {
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
            out.write("aString");
            out.close();
        } catch (IOException e) {
        }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: How to write to file java using bufferedwriter?

    Class BufferedWriter

    java.lang.Object
    == java.io.Writer
    == java.io.BufferedWriter


    public class BufferedWriter -- extends Writer

    Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

    a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

    Code:
    PrintWriter out
       = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
    will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to write to file java using bufferedwriter?

    Write text file

    Code:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.Writer;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        Writer writer = null;
    
        String text = "This is a text file";
    
        File file = new File("write.txt");
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(text);
        writer.close();
    
      }
    }

Similar Threads

  1. how to write this pyramid using java?
    By Rainy in forum Software Development
    Replies: 1
    Last Post: 07-09-2011, 02:30 PM
  2. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  3. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  4. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM
  5. Need example of fileoutputstream out.write to a file in Java.
    By Jerin m in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 11:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,042,045.86941 seconds with 16 queries