Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 862

Writing file in Java

$
0
0

Though Java provides classes like FileOutPutStream and FileWriter to write files using byte stream and character stream respectively but using them directly will slow down the I/O operation considerably.

It is always advisable to use BufferedOutputStream or BufferedWriter because that will provide buffering to the output streams and won't cause a call to the underlying system for each byte written. Buffered output streams write data to a buffer, and the native output API is called only when the buffer is full, making I/O operation more efficient.

It is same as reading file using BufferedReader where again the advantage of using buffered I/O streams is that; in case of Buffered input streams data is read from a memory area known as a buffer.

Writing file using BufferedOutputStream

BufferedOutputStream is a wrapper class over the OutputStream class and it adds buffering capability to the output stream.

Here one thing to note is – If your file contains character data, the best approach is to use character streams like BufferedWriter. Byte streams should only be used for the most primitive I/O.

The write method of the BufferedOuputStream takes either a byte array or an int as an argument thus you have to call getBytes() on any String that is passed to the write method.

Java code


import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileWriteDemo {

public static void main(String[] args) {
writeFileContent("G:\\test.txt");
}

/**
*
* @param fileName
*/
private static void writeFileContent(String fileName){
BufferedOutputStream bs = null;
try {
bs = new BufferedOutputStream(new FileOutputStream(fileName));
bs.write("Writing one line".getBytes());
// For windows, only \n for linux
bs.write("\r\n".getBytes());
bs.write("Writing second line".getBytes());

} catch (IOException ioExp) {
// TODO Auto-generated catch block
ioExp.printStackTrace();
}finally{
if(bs != null){
try {
bs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

Writing file using BufferedWriter

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

The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

Constructors

  • BufferedWriter(Writer out) - Creates a buffered character-output stream that uses a default-sized output buffer.
  • BufferedWriter(Writer out, int sz) - Creates a new buffered character-output stream that uses an output buffer of the given size.

Java code


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWrite {

public static void main(String[] args) {
writeFileContent("G:\\test1.txt");

}

/**
*
* @param fileName
*/
private static void writeFileContent(String fileName){
//BufferedWriter bw = null;
// Using try-with-resources here
try(BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
//bw = new BufferedWriter(new FileWriter(fileName));
bw.write("Writing one line");
bw.newLine();
bw.write("Writing second line");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Note that in this program try-with-resources is used to automatically manage resources. It is available from Java 7 and above.

That's all for this topic Writing file in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Reading delimited file in Java using Scanner
  2. How to read file from the last line in Java
  3. How to find last modified date of a file in Java
  4. Reading file in Java 8
  5. Zipping files in Java
  6. try-with-resources in Java 7

You may also like -

>>>Go to Java Programs page


Viewing all articles
Browse latest Browse all 862

Trending Articles