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

Java Example – Appending Data to a File

Java Example - Appending Data to a File

Java Examples Java Examples

The following example demonstrates how to use the FileWriter method to append data to a file:

Main.java File

import

java

.

io

.*;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

throws

Exception

{

try

{

BufferedWriter

out

=

new

BufferedWriter

(

new

FileWriter

(

"

filename

"

)

)

;

out

.

write

(

"

aString1

\n

"

)

;

out

.

close

(

)

;

out

=

new

BufferedWriter

(

new

FileWriter

(

"

filename

"

,

true

)

)

;

out

.

write

(

"

aString2

"

)

;

out

.

close

(

)

;

BufferedReader

in

=

new

BufferedReader

(

new

FileReader

(

"

filename

"

)

)

;

String

str

;

while

(

(

str

=

in

.

readLine

(

)

)

!=

null

)

{

System

.

out

.

println

(

str

)

;

}

in

.

close

(

)

;

}

catch

(

IOException

e

)

{

System

.

out

.

println

(

"

exception occoured

"

e

)

;

}

}

}

The output of the above code is:



aString1

aString2

Java Examples Java Examples