Ad

Combine A Read And Parse Text.txt

- 1 answer

Problem: I can't parse my file test.txt, by spaces. I can 1) read text files, and I can 2) parse strings, but I cannot connect the two and parse a text file! My purpose is to learn how to analyze text files. This is a simplified approach to that.

Progress: Thus far, I can read test.txt using FileReader and BufferedReader, and print it to console. Further, I can parse simple String variables. The individual operations run, but I'm struggling with parsing an actual text file. I believe this is because my test.txt is stored in the buffer, and after I .close() it, I can't print it.

Text File Content: This is a text file created, only for testing purposes.

Code:

import java.io.*;

public class ReadFile {

    //create method to split text file, call this from main
    public void splitIt(String toTest)
    {
        String[] result = toTest.split(" ");

        for (String piece:result)
        {
            //loop through the array and print each piece
            System.out.print(piece);
        }
    }


    public static void main(String[] args) {

        //create readfile method    
        try
        {
            File test = new File("C:\\final\\test.txt");
            FileReader fileReader = new FileReader(test);
            BufferedReader reader = new BufferedReader(fileReader);

            String line = null;

            //While there are still lines to be read, read and print them
            while((line = reader.readLine()) != null)
            {
                System.out.println(line);
                splitIt(line);
            }

            reader.close();
        }

        //Catch those errors!
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

//      readFileMethod a = new readFileMethod(line);
        System.out.println(a.splitIt());

    }

}

Preemptive thank you for your sharing your knowledge. Many posts on reading and parsing have been solved here on SO, but I've not the understanding to implement others' solutions. Please excuse me, I've only been learning Java a few months and still struggle with the basics.

Ad

Answer

Building on Scary Wombat and your code, i made some changes. It should now print the Line that is being read in and each word that is separated by space.

import java.io.*;

public class ReadFile {

    //create method to split text file, call this from main
    public static void splitIt(String toTest)
    {
        String[] result = toTest.split(" ");

        for (String piece:result)
        {
            //loop through the array and print each piece
            System.out.println(piece);

        }
    }


    public static void main(String[] args) {

        //create readfile method
        try
        {
            File test = new File("C:\\final\\test.txt");
            FileReader fileReader = new FileReader(test);
            BufferedReader reader = new BufferedReader(fileReader);

            String line = null;

            //While there are still lines to be read, read and print them
            while((line = reader.readLine()) != null)
            {
                System.out.println(line); // print the current line
                splitIt(line);
            }

            reader.close();
        }

        //Catch those errors!
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }

}
Ad
source: stackoverflow.com
Ad