Results 1 to 7 of 7

Thread: Reading Lines

  1. #1
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default Reading Lines

    Is there a way to make a program read text from a file one line at a time and manipulating that line before reading the next line?

    An example would be, taking the line from the file and adding it to another file.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Read about Scanner:
    Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)
    Scanner (Java 2 Platform SE 5.0)

    then use

    Scanner.nextLine() to take one line at a time,

    after each call of 'nextLine()' use your own methods to process the data before the loop comes back around to 'nextLine()' again.

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    In addition to ozzyman's suggestion you can try BufferedReader also as shown below
    Java Code:
    		File myFile = new File("D://premdas/Desktop/file_tutorials/parseme.txt");
    
    		try {
    			FileReader fr = new FileReader(myFile);
    			BufferedReader br = new BufferedReader(fr);
    			String line;
    			while((line = br.readLine()) != null)
    				System.out.println(line);
    				[I][COLOR="Green"]//do your manipulation here for each line[/COLOR][/I]
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    is one better than the other? which one is more common? i have heard about the burreredreader, but have never used it.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I like to use a scanner when getting input from command line, and buffered reader for reading files. I am not sure which is better(I believe, for files the buffered reader is better).

    I think the i/o tutorials go over which to prefer.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by droidus View Post
    is one better than the other? which one is more common? i have heard about the burreredreader, but have never used it.
    Nor a Scanner, nor a BufferedReader care where their input comes from (see their constructors (*)). I believe that both classes have to do some buffering which doesn't harm normally. I prefer a BufferedReader if all I have to do is read lines (the other Readers are just too stupid to do it ;-)

    kind regards,

    Jos

    (*) for convenience a Scanner can also directy take a File as its constructor parameter, but both can take Readers as their constructor parameters which is nice (imho)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default Thankyou

    Thankyou everyone for helping me with this problem. I am more aware of BufferedReader so that is what I will use.

Similar Threads

  1. reading lines from URL and printing backwards
    By luxurymode in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2011, 08:18 AM
  2. Two '\n' characters between lines while reading File
    By subith86 in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 09:56 AM
  3. problem reading a files lines
    By j187 in forum New To Java
    Replies: 1
    Last Post: 12-17-2010, 12:43 AM
  4. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  5. Reading specific lines
    By ivvgangadhar in forum New To Java
    Replies: 8
    Last Post: 01-12-2009, 08:53 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
  •