Results 1 to 5 of 5
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Talking Reading in Text Files

    I have a file that I need to read it, but I want to read it starting at the 8th line.

    How do I start at the 8th line as opposed to the 1st?
    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    
    public class GameBoard {
                        
            private BufferedReader inputStream; 
            
            private Tile [][] gameTiles;
            
            
            
            public GameBoard (String fileName) throws IOException {
            
            
            gameTiles = new Tile [5][5];
            String[] data = new String[100];
            
            for (int row = 0; row < 5; row++)
              for (int col = 0; col < 5; col++)
              {
              Tile temp = new Tile ("question", "answer", 99);
              gameTiles[row][col] = temp;
              } 
            
            
            try {
    
                inputStream = new BufferedReader(new FileReader("Tile.txt"));
    
                String line;
                int lineCount = 0;
                
                while ((line = inputStream.readLine()) != null) {
                    
                      System.out.println ("\nLine: " + lineCount + ": " + line);                
                      String thisLine = line;
                      data[lineCount] = line;
                      lineCount++;
                }
            } 
              
            finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                
                            System.out.println ("\n\nQuestion: " + data[0]);
                            System.out.println ("Answer: " + data[1]);
                            System.out.println ("Value: " + data[2]);
                      
              
            }
            
            
        }// end constructor
        
        public Tile getTile (int r, int c)
        {
        return gameTiles [r] [c];
        }
        
    }

  2. #2
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    also is there a way to read only 1 or 2 lines of code from a text file?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    If you only want to read 1 line then only invoke the readLine() method once.

  4. #4
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    oh...

    wait so does the readLine() method have a counting system? that lets it know what line to read?
    and what does it return? string, int, char, ect. Does it return whatever is in the text file


    Basically what i want to do is read in the first two lines of my text file first
    then later in my code read in where i stopped reading; will the method know to start where I stopped earlier or will it start over from the beginning

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by blueduiker View Post
    oh...

    wait so does the readLine() method have a counting system? that lets it know what line to read?
    and what does it return? string, int, char, ect. Does it return whatever is in the text file


    Basically what i want to do is read in the first two lines of my text file first
    then later in my code read in where i stopped reading; will the method know to start where I stopped earlier or will it start over from the beginning
    readLine() returns a String -- whatever text is on that line, minus the ending \n character. It doesn't have its own counting system, but of course you can keep track of how many times you call the method yourself. Your BufferedReader will keep track of where it is in the file, as long as it's alive. If it's closed or goes out of scope, it won't be able to keep track of anything, of course.

    -Gary-

Similar Threads

  1. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 05:45 AM
  2. Reading external text files from an Applet
    By Arnold in forum New To Java
    Replies: 2
    Last Post: 01-07-2010, 06:43 PM
  3. Reg: Reading Files from Text file
    By balaji csc in forum New To Java
    Replies: 1
    Last Post: 11-09-2009, 11:06 AM
  4. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM
  5. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 03:15 AM

Posting Permissions

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