Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How can I continuously read a text file in java

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I continuously read a text file in java

    Hi,

    My java code reads data from a text file stored in google drive. I'm trying to figure out how to continuously read the file data and once there is a new line added, output the line automatically. I am trying it by using sleep thread, but it is not working. I am attaching my code below:

    package remotedata;

    import java.awt.*;
    import java.net.*;
    import java.io.*;

    public class remotedataread extends Frame
    {
    public static void main(String[] args)
    throws InterruptedException, IOException{

    BufferedReader br = null;
    TextArea FileText =
    new TextArea(" Content of the File \'temp1.txt\' :");

    try
    {
    URL url =
    new URL("https://docs.google.com/uc?id=0B9MOgXFCss2iSWpicmVKSW9OOWM&export");

    InputStream is = url.openStream();
    br = new BufferedReader(new InputStreamReader(is));
    /* String line = null;
    while (true) {
    line = br.readLine();
    if (line == null) {
    //wait until there is more of the file for us to read
    Thread.sleep(1000);
    }
    else {
    System.out.println(line);
    }
    }*/
    }


    catch (MalformedURLException e)
    {
    System.out.println("Bad URL");
    }
    catch (IOException e)
    {
    System.out.println("IO Error : "+e.getMessage());
    }

    FileText.setBackground(Color.white);
    FileText.append(String.valueOf('\n'));

    Frame f = new Frame("server data");
    f.setSize(200,200);
    f.add(FileText);
    f.setVisible(true);

    try
    {
    String s;
    s=null;
    boolean eof = false;
    //while (true) {
    s = br.readLine();
    System.out.println("Time Temperature");
    while( !eof )
    {
    FileText.append(s + String.valueOf('\n'));
    try
    {
    //while (true) {
    s = br.readLine();
    if ( s == null )
    {
    // eof = true;
    // br.close();
    Thread.sleep(1000);


    }
    else{
    //System.out.println("Time Temperature");
    System.out.println(s);
    }

    //}

    }

    catch(InterruptedException e){
    System.out.println("got interrupted!");
    }

    catch (EOFException eo)
    {
    eof = true;
    }
    catch (IOException e)
    {
    System.out.println("IO Error : "+e.getMessage());
    }
    }
    //}

    }
    catch (IOException e)
    {
    System.out.println("IO Error : "+e.getMessage());
    }

    }

    }


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I continuously read a text file in java

    First off you need to put your code into a code tag to make it easier to read and to take up less space. Secondly I'm just trying to get what you are trying to do. Are you just reading a file in from a google doc then simply outputting it on the screen?

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I continuously read a text file in java

    This is a simple code which can read file from google docs and output the file content in the screen. but when my text file will be updated the output needs to be updated automatically.As I am a new member I do not know the proper way of sumitting question. I will submit question in a correct format from the next time.

Similar Threads

  1. [SOLVED] How to read and shuffle bytes from text file in Java....?'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: September 21st, 2012, 10:22 AM
  2. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  3. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM

Tags for this Thread