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 -
Category
Documents
-
view
212 -
download
0
description
Java class to append data
Transcript of 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) + " ");}
}}
}}