Results 1 to 6 of 6

Thread: Closing the bufferedreader

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Closing the bufferedreader

    Hello,
    I use to retrieve data from web pages a BufferedReader
    (BufferedReader stream = new BufferedReader (new InputStreamReader (u.openStream (), "utf-8")); u as a url of course).
    This works very well but when I try to work on large amount of data it fails. I created a new bufferdReader every time with my new url but it seems that after one time reading is becoming slower. I suspect that the bufferedreader method is not closed properly in the code. Is there something else to do?

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Closing the bufferedreader

    Hello,
    I think the following code might be helpful to you. Just have a look at it.
    Code:
      FileReader mfl = null;
    BufferedReader buff = null;
    		
    		try {
    			mfl = new FileReader(path);
    buff = new BufferedReader(mfl);
    
    Dump.setText("");
    String ln = null;
    			
    			while ((ln = buff.readLine())!=null) {
    				
    				Dump.setText(Dump.getText()+ Onln +"\ n");
    				
    			} 
    		} catch (IOException exception) {
    			exception.printStackTrace();
    		} finally {
    			try {
    				buff.close();
    mfl.close();
    			} catch(IOException exception1) {
    				exception1.printStackTrace();
    			}

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Closing the bufferedreader

    Hey
    I think that's wrong,
    Dump.setText(Dump.getText()+ Online +"\ n");
    The method is unnecessary here, if is of no use. This can be skipped. If I am not wrong the program should work smoothly even eliminating this method. Still, if you are using this method and are happy using this then you can go with it. And if you are not using it, I have an alternative to it.
    Last edited by opaper; 27-01-2010 at 12:09 PM.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Closing the bufferedreader

    Hello,
    Dump.setText(Dump.getText()+ Online +"\ n");
    I think the problem is in this statement. At each iteration of a new chain recreates a little larger, so that you allocate a huge amount of temporary memory. For example, if you read a file containing 1000 lines of 80 characters (80 KB), you actually allocates more because you have created a chain of 80, then 160, then 240 ... come up to 80,000 characters containing all your files. And actually if you add it all you will get over 39 MB of temporary channels, all this to read a file of 80 KB (and I do not even explain to you if the file is larger that it can take).

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Closing the bufferedreader

    Hello,
    If you want to overcome the above, then you can copy the file to a StringBuffer (or StringBuilder from Java ), Which will allow you to better manage memory. If I have any other comment on your code:
    1) You can include the reader into each other, and so you avoid many unnecessary references trainer.
    2) Use instead System.getProperty ( "line.separator") that "\ n" for line endings may be different from one system to another.
    3) Respects the naming conventions: no capitals at the beginning of variable name / attributes (Dump dump).

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Closing the bufferedreader

    Hello,
    Yes, I agree with the above posted. And if you want an example for it, then here it is
    Code:
    BufferedReader buff = null;
    		
    		try {
    			buff = new BufferedReader(new FileReader(path));
    
    StringBuffer buff = new StringBuffer();
    			/ / End of ln-specific system
    			Endl = String System.getProperty("ln.separator");
    String ln = null;
    			
    			while ((ln = buff.readLine())!=null) {
    				buff.append(row).append(endl);
    			}
    			Dump.setText(buff.function toString() {
        [native code]
    }());
    		} catch (IOException exception) {
    			exception.printStackTrace();
    		} finally {
    			try {
    				buff.close();
    			} catch(IOException exception1) {
    				exception1.printStackTrace();
    			}
    		}

Similar Threads

  1. Mail.app will not reopen after closing app
    By NewComer in forum Portable Devices
    Replies: 5
    Last Post: 19-06-2010, 05:11 PM
  2. BufferedReader can not find file
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 05:14 AM
  3. Problems with BufferedReader
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 11:42 AM
  4. Problem in BufferedReader
    By SoftWore in forum Software Development
    Replies: 3
    Last Post: 02-12-2009, 01:18 PM
  5. Closing Ceremony Of IPL
    By Raaj in forum Off Topic Chat
    Replies: 4
    Last Post: 04-06-2008, 12:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,493,695.65430 seconds with 17 queries