Java Example - Writing to a File
The following example demonstrates how to write content to a file using the write() method:
Example
/*
author by runoob.com Main.java
*/
import
java
.
io
.*;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
try
{
BufferedWriter
out
=
new
BufferedWriter
(
new
FileWriter
(
"
runoob.txt
"
)
)
;
out
.
write
(
"
Runoob Tutorial
"
)
;
out
.
close
(
)
;
System
.
out
.
println
(
"
File created successfully!
"
)
;
}
catch
(
IOException
e
)
{
}
}
}
The output of the above code is:
文件创建成功!
After successful creation, a file named runoob.txt will be generated in the current directory and the string "Runoob Tutorial" will be written to it.
Java Examples