append data

1
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 data randAccess.seek(randAccess.length()); // Move the file pointer to the end outP.writeBytes("\n" + "jam"); } } else { //-------- New file creation try ( FileOutputStream output = new FileOutputStream(file, true); PrintWriter printOut = new PrintWriter(file); ) { for (int i = 1; i <= 100; i++) { printOut.print((int) (Math.random() * 100) + " "); } } } } }

Upload: kevin-morton

Post on 10-Dec-2015

212 views

Category:

Documents


0 download

DESCRIPTION

Java class to 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) + " ");}

}}

}}