Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Stepping through a text file line by line

    I'm attempting to write a program that steps through lines from a text file (to be used in conjunction with MaxMsp. I'd like to read a whole text file and output a single line, then trigger the output of the next line.

    I'm not sure if there is a way to tokenize the lines, and recall them later. Or perhaps I should read them into an array and display the arrays.

    I appreciate any help that can be offered.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The problem with reading in the entire file all at once is you can run out of memory if it is a large file. Much easier is to use a Scanner to read the file line by line.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm attempting to write a program that steps through lines from a text file

    Create a buffered reader and repeatedly call its readLine() method. Java Developers ALmanac have an example.

    I'd like to read a whole text file and output a single line, then trigger the output of the next line.

    Why? I ask because this is a rather different aim from that expressed above. If you go this route you would read the entire textual content (and hope that it's not too large) and then tokenise the resulting String as you suggest. The String split() method will give you an array of lines - but knowing what to use as the regex argument will depend on the format of the text file (and possibly what you intend doing with empty lines etc)

    [Edit] Or you could create a list of strings and populate it with readLine() as described above.

  4. #4
    cps1 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Here is a file I wrote that reads a text file line by line.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class IOPractice
    {
    	public static void main(String[] args) throws IOException
    	{
    		Scanner s = new Scanner(System.in);
    		
    		File f = new File("file.txt");
    		Scanner numScan = new Scanner(f);
    		
    		String line;
    		
    		while (numScan.hasNext())
    		{
    			line = numScan.nextLine();
    			System.out.print(line + " ");
    		}
    		
    		
    	}
    }
    In line
    Java Code:
    File f = new File("file.txt");
    you'll want to enter the filename of a text file in the same folder.

    Hope that helps.

  5. #5
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    pbrockway2,

    I'm trying to send each line from a text file (a book) out to be processes by another programming environment. I don't care too much how this happens inside of java, but I'd like it to happen.

    I don't understand how to call the readline() from a buffered reader, but this sounds like a good approach.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I don't understand how to call the readline() from a buffered reader, but this sounds like a good approach.

    Did you try (and understand) the Almanac example? It does exactly what you are describing.

  7. #7
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I did try, unfortunately my skill with java are so weak I was unable to understand the code. Right now my non-working model is as basic as this

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

    public class Reader {

    void readLine(String fileName)
    {
    java.io.BufferedReader br = null;
    try {
    BufferedReader in = new BufferedReader(new FileReader("/users/evanlivingston/Desktop/Text.txt"));
    String str;
    while ((str = in.readLine()) != null) {
    System.out.println(str);
    }
    in.close();
    } catch (IOException e) {
    }
    }
    }

  8. #8
    cps1 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    A StringBuffer is not required. If you are just displaying it, import the Scanner class and create an instance of it. A simple while loop will iterate through the entire contents of the text file. Just make sure the text file is in the same directory as the java file.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Right now my non-working model is as basic as this

    So what happens when you compile and run the code?

    Also, if you don't understand the example, or aren't familiar with the overall structure of a Java program with its main() method and so on, perhaps you should start with the basics: a book, or Oracle's Tutorial. I don't say this to be discouraging but, rather, becuase I believe it's the fastest way to make progress.

  10. #10
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    When I compile the program displays the entire contents of a text file. I'm not sure how to specify to read just one line. I don't understand how to modify the readline() nor can I find examples doing this.

    Thanks again.

    (I'm working through tutorials, unfortunately I'm only now learning how to add variables and print the output) I'm having a hard time find tutorials or books which discuss the most basic elements of java.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    When I compile the program displays the entire contents of a text file.

    The code you last posted doesn't do this because it doesn't run. (it lacks a main() method)

    Post the code that displays the entire contents when run.

Similar Threads

  1. Replies: 8
    Last Post: 12-28-2010, 02:24 PM
  2. Inserting line into text file
    By Onra in forum Advanced Java
    Replies: 5
    Last Post: 09-14-2010, 11:21 AM
  3. Help! - How to insert a new line to a text file
    By matpj in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 05:28 PM
  4. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  5. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •