#Using a file (beginner)

1 messages · Page 1 of 1 (latest)

peak wagon
#

I have this code for opening a file in java and I am wondering how I can use the contents of the file within other methods that I am planning to create? I would like to check aspects about the file but unsure how to pass the file content into the method.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class TextAnalysis {

    public static void main(String[] args) {

        // read the text file 
        readFile();

    }


    public static void readFile() {

        String fileName = "PoliticalSpeech.txt";

        String line = null;

        try {
            FileReader fileReader = 
                new FileReader(fileName);

            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                
            }   

            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
        }

    }
loud harnessBOT
#

<@&987246399047479336> please have a look, thanks.

young imp
#

you could use Files.readAllLines or Files.readString to read the contents of the file a bit easier

#

use a field variable to reference that data, so you can use it across different methods within the class

peak wagon
young kettle
#

PoliticalSpeech.txt? Are you making a markov chain speech generator?

peak wagon
young kettle
#

This will give you a List of strings, one for each line (but not each necessarily each sentence.)

peak wagon
# young kettle This will give you a List of strings, one for each line (but not each necessaril...

Thank you! I ended up doing this:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; 
import java.io.FileNotFoundException;
import java.io.IOException;


public class TextAnalysis {



    public static void main(String[] args) {

        // Read the text file 
        String text = readFile();

        // Count the number of words in the file
        int numWords = countWords(text); 
        System.out.println(numWords + " words in the text file.\n"); 

        // Count the number of sentences in the file 
        int numSentences = countSentences(text);
        System.out.println(numSentences + " sentences in the text file.\n"); 


    }

    public static String readFile() {
        /* Reads a textfile into a string 
         * 
         * @param None 
         * @return string, the content of the text file 
         */


        Path fileName = Paths.get("PoliticalSpeech.txt"); 

        try {
            // Read the text file 
            String content = Files.readString(fileName); 
            return content; 
        }

        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
        }

        return ""; 

    }

loud harnessBOT