Results 1 to 20 of 20
  1. #1
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Reading a TxtFile from a Server

    Hi!

    I have a really annoying problem that i cant seem to find the reason to.

    I am trying to read a textfile from a server (then Ill split the rows and make something fun with it, but thats a problem for the future)
    My problem is... When the file is short, I can read it in... (like 10-20 rows...
    But when the file is a couple of hundred rows long the reading just freezes in the "middle" of the file(not exactly in the middle but a number of rows into the file...
    I have tried to use a Scanner to read the file... and it froze in the middle of the file... Then i used this code:
    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.ArrayList;
    
    public class UsersOnline{
        public UsersOnline(ArrayList servers){
            try{
            URL u = new URL("http://de1.www.ivao.aero/whazzup.txt");
            URLConnection uc = u.openConnection();
            InputStream raw = uc.getInputStream();
            InputStream buffer = new BufferedInputStream(raw);
            Reader r = new InputStreamReader(buffer);
            int c;
            while((c = r.read()) !=-1){
                System.out.print((char)c);
            }
            }
            catch (MalformedURLException ex){
                System.err.println("FEL URL");
            }
            catch (IOException ex){
                System.err.println(ex);
                        
            }
            }
        }
    But its the same result with this code. It just stops printing out in the middle of the textfile.... No exception or errors or anything... it just stops...(netbeans tells me that the application is still running...)

    I am out of ideas... all help is appreciated:-)

    /Niklas Eriksson

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    The code works for me. It reads cnt=262955 characters

  3. #3
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    This is so strange, at my place it stops... no mather what ive tried, ive tried to run it in netbeans... im the command prompt... but still no luck!:(

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    How many char does it read before stopping?
    Change the println to
    Java Code:
               int cnt = 0;
               while((c = r.read()) !=-1){
                  cnt++;
    //                System.out.print((char)c);
               }
               System.out.println("cnt=" + cnt);   // cnt=263465 cnt=262955 cnt=262068

  5. #5
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    I tried exactly that... But it never comes down to System.out.println("cnt= "+cnt),

    This is the last line that gets printed out:
    'AFR6936:386326:Alexandre Didone LFLL:PILOT::45.718:3.12666:18017:415:1/A320/M-SDRWY/C:N0330:LFLC:F180:LFRS:EU6:B:3:5403:0:50:1:I:1841: 1841:11:0:3:0:LFRN::GERVA V13 LARON DCT GUSON POI R66 LUGEN:::::::20120217183644:IvAp:1.9.8:2:3::G:120:2 95:0:9
    AFR718:382783:Ezekiel Lejeune LFPG:PILOT::23.4579:-12.2059:36950:452:1/B772/H-SDGPRTWY/S:M084:LFPG:F350:GOOY:EU2:B:4:5302:0:50:1:I:1515:1 515:5:40:7:0:ZZZZ:OPR/ AIR FRANCE VIRTUEL DOF/120217 RMK/ FMC AND CHARTS ON BOARD STEP CLIMB:AGOPA UL167 ARKIP UQ237 LMG UN10 SAU UN857 BAN UN10 CJN UL27 BLN UN869 ECHED/F370M084 ADM UR975 SLO:::::::201

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    Comment out the printing of the characters. See line 4.

    Change line 3 to print a line every 20000 characters. Change the value to find the total.
    if (cnt++ % 20000 == 1) System.out.println("cnt=" + cnt);

  7. #7
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    Sorry, i cant get that to work...
    My code looks like this now... :-)
    Reader r = new InputStreamReader(in);
    int c;
    int cnt = 0;

    System.out.println("cnt=" + cnt); // cnt=263465 cnt=262955 cnt=262068
    while((c = r.read()) !=-1){
    if (cnt++% 2000 ==1){
    System.out.println("cnt ="+cnt);
    }
    // System.out.print((char)c);
    }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    i cant get that to work...
    Please explain. Post the error mesages.

    Java Code:
     
         
    	        while((c = r.read()) !=-1){
                        if (cnt++ % 20000 == 1){    //<<<<<<<< 20000 not 2000
                           System.out.println("cnt ="+cnt);
                        }
    	        //    System.out.print((char)c);
    	        }
                    System.out.println("cnt=" + cnt);

  9. #9
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    This is the error message... Its a runtime error this time;)
    java.lang.UnsupportedClassVersionError: UsersOnline : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)

    Probably the code is wrong...

    Reader r = new InputStreamReader(in);
    int c;
    int cnt = 0;

    System.out.println("cnt=" + cnt); // cnt=263465 cnt=262955 cnt=262068
    while((c = r.read()) !=-1){
    if (cnt++% 2000 ==1){
    System.out.println("cnt ="+cnt);
    }
    // System.out.print((char)c);
    }
    }
    catch (MalformedURLException ex){
    System.err.println("FEL URL");
    }
    catch (IOException ex){
    System.err.println(ex);

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    java.lang.UnsupportedClassVersionError: UsersOnline : Unsupported major.minor version 51.0
    You need to execute the code with the same version as you are using to compile it.
    one is 1.6 and the other is 1.7
    Last edited by Norm; 02-17-2012 at 09:36 PM.

  11. #11
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    Its strange, i use netbeans to write and run it... haha I have to try it on the other computer

  12. #12
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    Quote Originally Posted by Norm View Post
    You need to execute the code with the save version as you are using to compile it.
    one is 1.6 and the other is 1.7
    Ok now i changed back to my "original" computer and your code worked. sorry about that...

    Using your code i got this write-out
    Ok now that thing worked... for me using your code it writes: cnt =2
    cnt =20002
    cnt =40002
    cnt =60002
    cnt =80002
    and then it just says nothing...

    The next time i ran your code i got:
    cnt =2
    cnt =20002
    cnt =40002
    cnt =60002
    cnt =80002
    cnt =100002
    cnt =120002

    And the next time i ran it I got:
    run:
    cnt =2
    cnt =20002
    cnt =40002
    cnt =60002

    So it seems to stop at different places all the time...
    Last edited by Nigge; 02-17-2012 at 09:33 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    I have no idea why the program hangs for you. I seems to work for me.

  14. #14
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    I See... this is very very very very strange and annoying... Thanks anyway for your help:-)
    If you or anyone else comes up with a suggestion im more than happy to hear it:)

  15. #15
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    Quote Originally Posted by Norm View Post
    I have no idea why the program hangs for you. I seems to work for me.
    My problem seems to be that when i run the program on my laptop it freezes, but when i use if from my original computer it works...
    (the laptop has a wireless connection) So my guess is that the wireless connection is too slow to make it work...
    What my code needs is that the txtfile is downloaded to the hardrive BEFORE it is being accessed...

    Can anyone help me with this?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    txtfile is downloaded to the hardrive BEFORE it is being accessed...
    Use a browser to download and save the file to the harddisk before trying to read it in your program.

  17. #17
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    I need it to work on other computers than myself... and i need to download the file each 10 minutes... so i need the program to download the file itself...

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    You'll have to find out why the laptop freezes if you want to read the file with it.

  19. #19
    Nigge is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Reading a TxtFile from a Server

    I think that it might be that im using a wireless connection on the laptop and a wired connection on the big computer. So that the connection from the wired computer is much faster than the wireless...
    So if its possible to download the file to the harddrive of the computer and THEN read it... instead of reading from the server as I do know... I think the problem would be solved... my problem is how i do this...:S

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a TxtFile from a Server

    if its possible to download the file to the harddrive of the computer
    That is what the code put posted would do if you added code to it to write the file to the harddrive.

    Did you try a browser to download the file?

Similar Threads

  1. Reading object in multithreaded socket server
    By dario111cro in forum Networking
    Replies: 1
    Last Post: 02-06-2012, 11:04 PM
  2. Problem reading from server socket
    By glauber in forum Advanced Java
    Replies: 5
    Last Post: 02-17-2011, 12:11 PM
  3. reading URL using java through proxy server
    By asheesh in forum Networking
    Replies: 16
    Last Post: 04-25-2010, 02:15 PM
  4. Replies: 0
    Last Post: 04-29-2009, 07:16 PM
  5. [SOLVED] Applet reading from file on server
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-26-2009, 11:33 PM

Posting Permissions

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