Results 1 to 20 of 20
Thread: Reading a TxtFile from a Server
- 02-17-2012, 06:19 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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:
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...)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); } } }
I am out of ideas... all help is appreciated:-)
/Niklas Eriksson
- 02-17-2012, 07:52 PM #2
Re: Reading a TxtFile from a Server
The code works for me. It reads cnt=262955 characters
- 02-17-2012, 08:00 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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!:(
- 02-17-2012, 08:05 PM #4
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
- 02-17-2012, 08:09 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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
- 02-17-2012, 08:20 PM #6
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);
- 02-17-2012, 08:31 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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);
}
}
- 02-17-2012, 08:36 PM #8
Re: Reading a TxtFile from a Server
Please explain. Post the error mesages.i cant get that to work...
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);
- 02-17-2012, 08:38 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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);
- 02-17-2012, 08:46 PM #10
Re: Reading a TxtFile from a Server
You need to execute the code with the same version as you are using to compile it.java.lang.UnsupportedClassVersionError: UsersOnline : Unsupported major.minor version 51.0
one is 1.6 and the other is 1.7Last edited by Norm; 02-17-2012 at 09:36 PM.
- 02-17-2012, 08:47 PM #11
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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
- 02-17-2012, 09:30 PM #12
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Reading a TxtFile from a Server
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.
- 02-17-2012, 09:37 PM #13
Re: Reading a TxtFile from a Server
I have no idea why the program hangs for you. I seems to work for me.
- 02-17-2012, 09:41 PM #14
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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:)
- 02-18-2012, 01:01 AM #15
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Reading a TxtFile from a Server
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?
- 02-18-2012, 01:40 AM #16
Re: Reading a TxtFile from a Server
Use a browser to download and save the file to the harddisk before trying to read it in your program.txtfile is downloaded to the hardrive BEFORE it is being accessed...
- 02-18-2012, 01:47 AM #17
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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...
- 02-18-2012, 02:07 AM #18
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.
- 02-18-2012, 02:12 AM #19
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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
- 02-18-2012, 02:17 AM #20
Similar Threads
-
Reading object in multithreaded socket server
By dario111cro in forum NetworkingReplies: 1Last Post: 02-06-2012, 11:04 PM -
Problem reading from server socket
By glauber in forum Advanced JavaReplies: 5Last Post: 02-17-2011, 12:11 PM -
reading URL using java through proxy server
By asheesh in forum NetworkingReplies: 16Last Post: 04-25-2010, 02:15 PM -
reading files from the server where the Directory listings are disabled
By sudukrish in forum Advanced JavaReplies: 0Last Post: 04-29-2009, 07:16 PM -
[SOLVED] Applet reading from file on server
By DenniGa in forum Java AppletsReplies: 3Last Post: 02-26-2009, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks