/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteReadData.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.mycompany.lab6.exceptions_calebfontenot;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
@author
public class WriteReadData {
public static void main(String[] args) throws Exception
{
File file = new File("rw.txt");
int randInt;
if (!file.exists()) {
System.out.println("File does not exist! Creating it...");
try (PrintWriter output = new PrintWriter(file);) {
for (int i = 0; i < 100; ++i) {
randInt = (int) (Math.random() * 100);
System.out.println("Writing " + randInt + " to file...");
output.write(Integer.toString(randInt) + "\n");
}
} catch (Exception ex) {
System.out.println(ex);
}
}
Scanner input = new Scanner(file);
System.out.println("Reading file contents:");
while (input.hasNext()) {
System.out.println(input.next());
}
}
}