0

Objective & Issue

Objective of the task I am trying to accomplish is to locate and read a text file from a provided path then copy each line of the text file into a ArrayList<String>. This "ApplicationFileReader" object is created in some control class by calling the constructor which takes in the path (it is sent the String, "TextFile.txt" [the name of a FILLED text file within the src folder]).

However, whenever the program runs, the ArrayList<String> is still empty after reading the path file. The code does not throw an IOException which might mean that it was able to locate the file, just possibly not able to actually extract the lines of text into the list. (Code runs fine, just no output). Any reason for this? Should I abandon this method for the other simple ones like in this one (Although I am still curious about the issue).

Thanks in advance

Code Explanation

An ApplicationFileReader object consists of 3 instances: 2 Strings for the file path and all the text for the file and an ArrayList<String> for containing each line of the text file. Default constructor sets everything to be empty. The argument constructor (that takes one String) accomplishes setting all 3 instances by validation of the path and extraction of the lines of text.

Anything I'm missing? Please comment.

Code: ApplicationFileReader

//TODO: Send exception to LOGGER to print to console

package model;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class ApplicationFileReader {

    public String filePathLocation;
    public String fileText;
    public ArrayList<String> fileTextLines;
    
    public ApplicationFileReader(){
        filePathLocation = "";
        fileText = "";
        fileTextLines = new ArrayList<String>();
    }
    
    public ApplicationFileReader(String filePathLocation){
        this.filePathLocation = filePathLocation;
        this.fileText = "";
        fileTextLines = new ArrayList<String>();
        extractFileText(filePathLocation);
        
        /* Testing */
        for(String s : fileTextLines)
            System.out.println(s);
    }

    public boolean extractFileText(String filePathLocation) {
        if(!validateFilePathLocation(filePathLocation))
            return false;
        try {
            Path path_filePath = Paths.get(filePathLocation);
            fileTextLines.addAll(Files.readAllLines(path_filePath, StandardCharsets.UTF_8));
            for(String eachListLine : fileTextLines){
                fileText += eachListLine;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    private boolean validateFilePathLocation(String filePathLocation) {
        try {
            File temporaryFile = new File(filePathLocation);
            if(!temporaryFile.exists() && temporaryFile.isDirectory())
                throw new IOException();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
Community
  • 1
  • 1
codingbash
  • 1,102
  • 1
  • 9
  • 21

1 Answers1

0

You are not reading the file at all in your extractTextFile() method.

Files.write() is for writing the contents of the Arraylist to the file.

You may want to use Files.readAllLines(path);

Codebender
  • 14,221
  • 7
  • 48
  • 85