Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. package Documenthandler;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class TextFile {
  9.  
  10.  
  11. public void ReadTextFile() throws FileNotFoundException {
  12.  
  13. /**
  14. * creating String lines so we can fill the line with the text from textdocs.
  15. */
  16. String line = "";
  17. String line1 = "";
  18. String line2 = "";
  19.  
  20.  
  21. /**
  22. * creating scanners to read the lines from the 3 textdocs
  23. * using delimeters to break the code and handling white space.
  24. */
  25. Scanner scan = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/engwords.txt"))).useDelimiter(",\\s*");
  26. Scanner scan1 = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/numbers.txt"))).useDelimiter(",\\s*");
  27. Scanner scan2 = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/plaintext.txt"))).useDelimiter(",\\s*");
  28. ArrayList temps = new ArrayList();
  29.  
  30. /**
  31. * create while loop to read the whole text and implement it in String line
  32. * insert line into Arraylist temp.
  33. * close scanner so it prevent nested loop.
  34. */
  35. while (scan.hasNext()) {
  36. line = scan.next();
  37. temps.add(line);
  38. }
  39.  
  40. scan.close();
  41.  
  42. while (scan1.hasNext()) {
  43. line1 = scan1.next();
  44. temps.add(line1);
  45. }
  46.  
  47. scan1.close();
  48.  
  49. while (scan2.hasNext()) {
  50. line2 = scan2.next();
  51. temps.add(line2);
  52. }
  53.  
  54. scan2.close();
  55.  
  56. /**
  57. * creating a string array where we put elements from arraylist into string array [0], [1], [2] represent which files we wanna read from
  58. */
  59. String[] tempsArray = (String[]) temps.toArray(new String[0]);
  60. tempsArray = (String[]) temps.toArray(new String[1]);
  61. tempsArray = (String[]) temps.toArray(new String[2]);
  62. System.out.println(tempsArray[2]);
  63. }
  64.  
  65. /**
  66. * checking if the words able exist in test class
  67. * @throws FileNotFoundException
  68. */
  69. public void CheckFile() throws FileNotFoundException {
  70. Scanner scan = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/engwords.txt"))).useDelimiter(",\\s*");
  71. }
  72.  
  73. /**
  74. * testing if delimeter object works when inserting text into an String array and it did not!
  75. * @throws FileNotFoundException
  76. */
  77. public void CheckWithoutDelitMeter() throws FileNotFoundException {
  78. Scanner scan = new Scanner(new File("C:/textfile/src/DocumentHandlerTests/engwords.txt"));
  79. String line = "";
  80. ArrayList temps = new ArrayList();
  81.  
  82. while (scan.hasNext()) {
  83. line = scan.next();
  84. temps.add(line);
  85. }
  86.  
  87. scan.close();
  88. String[] tempsArray = (String[]) temps.toArray(new String[0]);
  89. System.out.println(tempsArray[0]);
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement