Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Writing to a File

Java Example - Writing to a File

Java Examples Java Examples

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 Java Examples