java.io
package.java.io.OutputStreamWriter
class.write()
methods allow you to write character(s) or strings to a file.PrintWriter
, which provide better performance and higher-level, more flexible methods to write data.FileWriter(File file)
: Creates a FileWriter
object using specified File object. It throws an IOException
if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.FileWriter(File file, boolean append)
: Creates a FileWriter object using specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. It throws an IOException if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.FileWriter(FileDescriptor fd)
: Creates a FileWriter object associated with specified file descriptor.FileWriter(String fileName)
: Creates a FileWriter object using specified fileName. It throws an IOException
if the named file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.FileWriter(String fileName, boolean append)
: Creates a FileWriter object using specified fileName with a boolean indicating whether or not to append the data written. If the second argument is true, then the data will be written to the end of the file rather than the beginning. It throws an IOException if the named file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.FileWriter inherits the method of java.io.OutputStreamWriter
and java.io.Writer
classes. Let’s have a look at the below methods with examples.
This method writes a single character specified by int c.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* Java write file using FileWriter write method
*
* @author pankaj
*
*/
public class FileWriterWriteIntExample {
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("D:/data/file.txt");
//inherited method from java.io.OutputStreamWriter
fileWriter.write(65);
fileWriter.write(66);
fileWriter.write(67);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fileWriter != null) {
fileWriter.flush();
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter implements AutoCloseable
interface, hence we can use try with resources while using FileWriter class.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write method using try with resource
*
* @author pankaj
*
*/
public class FileWriterWriteIntTryWithResource {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.OutputStreamWriter
fileWriter.write(65);
fileWriter.write(66);
fileWriter.write(67);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: In the above program fileWriter.write(65)
will write A into file because the 65 is the decimal value for the character A, hence integer 65 will be converted into character A and same for the other.
This method writes a portion of String str
from int off
to int len
.
If the value of the len parameter is negative then no characters are written.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(String s, int off, int len) method
*
* @author pankaj
*
*/
public class FileWriterWriteStringExample {
public static void main(String[] args) {
String data = "This is FileWriter Example.";
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.OutputStreamWriter
fileWriter.write(data, 8, 10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This method writes a portion of an array of characters specified by char[] cbuf
from int off
to int len
.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(char[] cbuf, int off, int len) method
*
* @author pankaj
*
*/
public class FileWriterWriteCharArray {
public static void main(String[] args) {
char[] data = "This is FileWriter Example.".toCharArray();
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.OutputStreamWriter
fileWriter.write(data, 8, 10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This method writes the array of character specified by cbuf.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(char[] cbuf) method
*
* @author pankaj
*
*/
public class FileWriterWriteCharArrayExample {
public static void main(String[] args) {
char[] data = "This is FileWriter Example.".toCharArray();
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This method writes a string value into file specified by str
.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(String str) method
*
* @author pankaj
*
*/
public class FileWriterWriteString {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
} catch (Exception e) {
e.printStackTrace();
}
}
}
This method appends the specified character to this writer where c
is the 16-bit character to append.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter append(char c) method
*
* @author pankaj
*
*/
public class FileWriterAppendCharacter {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
fileWriter.append('C');
} catch (Exception e) {
e.printStackTrace();
}
}
}
This method flushes the stream. When flush()
method is called it immediately writes the data to the output file.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file with FileWriter flush() method
*
* @author pankaj
*
*/
public class FileWriterFlushExample {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
//inherited method from java.io.OutputStreamWriter
fileWriter.flush();
fileWriter.write(" Tutorials");
fileWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
This method flush the stream before close it. Once the stream has been closed, invocation of write()
or flush()
method will cause an IOException
to be thrown. Closing a previously closed stream has no effect.
package com.journaldev.io.filewriter;
import java.io.FileWriter;
/**
* Java write file with FileWriter close() method
*
* @author pankaj
*
*/
public class FileWriterCloseExample {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
//inherited method from java.io.OutputStreamWriter
fileWriter.close();;
fileWriter.write(" Tutorials");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at com.journaldev.examples.FileWriterCloseExample.main(FileWriterCloseExample.java:20)
OutputStreamWriter
to accept strings.Also check java write file for more about how to write file in java. That’s all for Java FileWriter, I hope nothing important got missed here.
You can download all the examples code from our GitHub Repository.
Reference: API Doc
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.