Download - Append Data

Transcript
Page 1: Append Data

import java.io.*;

public class appendDataExample {public static void main(String[] args) throws IOException {

File file = new File("Exercise_17_01.txt");

if (file.exists()) {try (

RandomAccessFile randAccess = new RandomAccessFile(file, "rw");DataOutputStream outP = new DataOutputStream(new FileOutputStream(file,

true));) {

//-------- Append datarandAccess.seek(randAccess.length()); // Move the file pointer to

the endoutP.writeBytes("\n" + "jam");

} }

else {//-------- New file creationtry (

FileOutputStream output = new FileOutputStream(file, true);PrintWriter printOut = new PrintWriter(file);

) {for (int i = 1; i <= 100; i++) {

printOut.print((int) (Math.random() * 100) + " ");}

}}

}}