Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-07-2009
Member
 
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.
Reply With Quote
  #2  
Old 28-07-2009
Member
 
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) {
    }
Reply With Quote
  #3  
Old 28-07-2009
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
  #4  
Old 28-07-2009
Member
 
Join Date: May 2008
Posts: 2,383
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();

  }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 12:59 AM.