Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Documenthandler;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class TextFile {
- public void ReadTextFile() throws FileNotFoundException {
- /**
- * creating String lines so we can fill the line with the text from textdocs.
- */
- String line = "";
- String line1 = "";
- String line2 = "";
- /**
- * creating scanners to read the lines from the 3 textdocs
- * using delimeters to break the code and handling white space.
- */
- Scanner scan = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/engwords.txt"))).useDelimiter(",\\s*");
- Scanner scan1 = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/numbers.txt"))).useDelimiter(",\\s*");
- Scanner scan2 = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/plaintext.txt"))).useDelimiter(",\\s*");
- ArrayList temps = new ArrayList();
- /**
- * create while loop to read the whole text and implement it in String line
- * insert line into Arraylist temp.
- * close scanner so it prevent nested loop.
- */
- while (scan.hasNext()) {
- line = scan.next();
- temps.add(line);
- }
- scan.close();
- while (scan1.hasNext()) {
- line1 = scan1.next();
- temps.add(line1);
- }
- scan1.close();
- while (scan2.hasNext()) {
- line2 = scan2.next();
- temps.add(line2);
- }
- scan2.close();
- /**
- * creating a string array where we put elements from arraylist into string array [0], [1], [2] represent which files we wanna read from
- */
- String[] tempsArray = (String[]) temps.toArray(new String[0]);
- tempsArray = (String[]) temps.toArray(new String[1]);
- tempsArray = (String[]) temps.toArray(new String[2]);
- System.out.println(tempsArray[2]);
- }
- /**
- * checking if the words able exist in test class
- * @throws FileNotFoundException
- */
- public void CheckFile() throws FileNotFoundException {
- Scanner scan = (new Scanner(new File("C:/textfile/src/DocumentHandlerTests/engwords.txt"))).useDelimiter(",\\s*");
- }
- /**
- * testing if delimeter object works when inserting text into an String array and it did not!
- * @throws FileNotFoundException
- */
- public void CheckWithoutDelitMeter() throws FileNotFoundException {
- Scanner scan = new Scanner(new File("C:/textfile/src/DocumentHandlerTests/engwords.txt"));
- String line = "";
- ArrayList temps = new ArrayList();
- while (scan.hasNext()) {
- line = scan.next();
- temps.add(line);
- }
- scan.close();
- String[] tempsArray = (String[]) temps.toArray(new String[0]);
- System.out.println(tempsArray[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement