Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. package ex1textfile;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. public class Ex1TextFile {
  6.  
  7. public static void main(String[] args) throws FileNotFoundException {
  8. // Create an array list of string
  9. ArrayList<String> list = new ArrayList<>();
  10.  
  11. // Check if file exist
  12. File file = new File("Ex1TextFile.txt");
  13. if (file.exists()) {
  14. try (
  15. // Create input file
  16. Scanner input = new Scanner(file);
  17. ) {// Read data from file
  18. while (input.hasNext()) {
  19. list.add(input.nextLine());
  20. }
  21. }
  22. }
  23. // Generate 100 integers randomly
  24. for (int i = 0; i < 100; i++) {
  25. list.add(((int)(Math.random() * 100)) + " ");
  26. }
  27.  
  28. try (
  29. // Create output file
  30. PrintWriter output = new PrintWriter(file);
  31. ) {
  32. // write to file using text I/O
  33. for (String l: list) {
  34. output.print(l);
  35. }
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement