Results 1 to 6 of 6

Thread: Reading a file in loop

  1. #1
    Join Date
    Jul 2009
    Posts
    127

    Reading a file in loop

    Hello everyone,
    I have just started java I/O. I wonder if it was possible to read a file line by line and loop, without close it and reopen it each time? Is this possible? If it is possible then please do post back. An example will be appreciated. Thank you very much.
    Last edited by Xmen; 09-01-2010 at 02:25 PM.

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

    Re: Reading a file in loop

    Hi
    It is quite possible not to close the file. For example, to simulate a "tail-f". See the following example
    Code:
    BufferedReader bff = new BufferedReader(new FileReader("c:\\ex.txt "));
    		try {
    			while (true) {
    				String line;
    				while ( (line = bff.readLine()) != null ) {
    					System.out.System.out.println(line);
    				}
    				
    				while ( ! bff.ready() ) {
    					/ / With a bffeak to avoid a loop active:
    					Thread.sleep(100);
    				}
    			} 
    		} finally {
    			bff.close();
    		}
    By cons of course it only takes into account the changes made at the end of the file.

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

    Re: Reading a file in loop

    Hello,
    I would add, that memory, under windows, with automatic lock on the file open, it prevents another process to open the file for writing. I have tried this only for windows, I have no ideas how this works under linux operating system, I have not test on linux. Since the above program code will not close the file, the operating system will automatically lock the file and then it will close it.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Reading a file in loop

    Hello,
    I would add, that memory, under windows, with automatic lock on the file open, it prevents another process to open the file for writing ...
    This would make the remark that windows is a lot more different operating system than any other, and is it that it has some more features than any other operating system. I could not test, because ultimately that keeping open the flow is not possible in my case. (polling the syslog file with turning).

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

    Re: Reading a file in loop

    Hello,
    No it will prevent to delete or rename it, but we can always write in it. By cons that can block certain program that uses temporary files and uses the renaming when saving. Similarly, if we rewrite the file from the beginning we did not recover any data, but only that from the location where it was (if the file is longer then).
    Under Linux it is possible to delete / rename a file during its use of memory:
    1) If you rename the file mapping will always. Clearly it will continue to read the file with the new name as it has not closed (the same file).
    2) If we remove it we can continue to read it (but there will be nothing), but not on any file with the same name created thereafter.

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

    Re: Reading a file in loop

    Hello
    If we remove it we can continue to read it (but there will be nothing), but not on any file with the same name created thereafter.
    To disambiguate the sentence, there will be no file (disappeared), but the program that opened before the automatic continuous namely everything was inside. The file is separated from the file system but not yet released on disc.
    See the following:
    Code:
    lsof | grep ex.txt
    java      9080      delbd 3w REG        8,5   9555638      99166 / tmp / ex.txt (deleted)

Similar Threads

  1. Reading some file using C language
    By Vaikuntam in forum Software Development
    Replies: 9
    Last Post: 01-09-2012, 02:13 PM
  2. Reading XML file with ASP
    By Dharamsi in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:47 PM
  3. Reading properties of a file
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 14-02-2010, 05:46 AM
  4. Reading file bit by bit
    By John Wilson in forum Software Development
    Replies: 4
    Last Post: 14-12-2009, 01:49 PM
  5. Reading from INI file to several textbox in VB.NET
    By MABON in forum Software Development
    Replies: 5
    Last Post: 04-11-2009, 09:23 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,291,233.69140 seconds with 17 queries