Jump to content
  • 0

Readline() Java Problem Doesen't Read The Whole Line.


Question

Posted
			url = new URL(Config.WEB_LINK);
			is = url.openStream(); // throws an IOException
			br = new BufferedReader(new InputStreamReader(is));
			int count = 0;
			while ((line = br.readLine()) != null)
			{
				if (count == 0)
				{
					comanda = line;
				}
				count++;
			}
			System.out.println("DEBUG: Webcheck:" + comanda);

The file is a simple .txt file containing "Still doesen't work how it should."

 

First output: DEBUG: Webcheck:announce Still doesen't work how it s

Second output: DEBUG: Webcheck:announce Still doesen't work how it should.

 

I've made a thread to check every 60 seconds and the first time doesen't read the whole line, but the second time it read it.

Any idea why?

Recommended Posts

  • 0
Posted

U could try with

 

 

try
{
URLConnection conn = new URL(Config.WEB_LINK).openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
         BufferedReader in = new BufferedReader(isr);
String inputLine;
 
while ((inputLine = in.readLine()) != null)
{
System.out.println("DEBUG: Webcheck:" + inputLine);
}
in.close();
}
catch (Exception e)
{
System.out.println("Something went wrong:" + e);
}

Btw could u post the link of that html ;D

  • 0
Posted (edited)

U could try with

try
{
URLConnection conn = new URL(Config.WEB_LINK).openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
         BufferedReader in = new BufferedReader(isr);
String inputLine;
 
while ((inputLine = in.readLine()) != null)
{
System.out.println("DEBUG: Webcheck:" + inputLine);
}
in.close();
}
catch (Exception e)
{
System.out.println("Something went wrong:" + e);
}

Btw could u post the link of that html ;D

 

Is a simple txt file, not even html

Edited by TroubleChick
  • 0
Posted (edited)

Whatever but I was lazy to upload one to my ftp to test xD I will, then... q.q

 

P.S: for me it's warking what I posted

 

P.S 2: What you posted is also working for me, with the try() catch() that's missing...

Edited by ^Wyatt
  • 0
Posted (edited)

Whatever but I was lazy to upload one to my ftp to test xD I will, then... q.q

 

P.S: for me it's warking what I posted

I made a different class in eclipse to see if it's working and both ways it's working, BUT when i go in the gameserver first time doesent read all the line and second time it does. I don't get it.

Could be the format of TXT file or anything?!

 <?
if($_POST['Submit']){
$open = fopen("commands.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />"; 
echo "File:<br />";
$file = file("commands.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"30\" rows=\"1\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?> 

This is the PHP script that edit the file.

Edited by TroubleChick
  • 0
Posted (edited)

I don't think that is about file extension... but u can simply test it...

Btw how did u schedule the check, can u post it here? U could test adding some more seconds for the first check.

Edited by ^Wyatt
  • 0
Posted

ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new checkIt(), 60000, Config.WEB_INTERVAL * 1000);//

 

Everytime i change something at the commands.txt file sometimes it doesen't work from first time.

 

Weird Example: I test the new class with new text everytime and it reads whole. And if i read whole before the new check comes the check is reading whole too.

  • 0
Posted (edited)

But what has to do the php form with your code? I mean, your code is just checking the file, isn't it?

You could give me the link to see if I get your error too  :rage:

Edited by ^Wyatt
  • 0
Posted (edited)

Yes just checking the file and taking the first line. I don't get it why it loose ends of the line first time.

 

LE: Isn't 60 seconds enough?

 

OUTPUT1: DEBUG: Webcheck:announce i dont fucking get it.asda

OUTPUT2: DEBUG: Webcheck:announce i dont fucking get it.asdas

	public static String getContent()
	{
		String comanda = "";
		
		try
		{
			URLConnection conn = new URL(Config.WEB_LINK).openConnection();
			conn.addRequestProperty("User-Agent", "Mozilla/4.76");
			InputStreamReader isr = new InputStreamReader(conn.getInputStream());
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			int count = 0;
			
			while ((inputLine = in.readLine()) != null)
			{
				if (count == 0)
				{
					System.out.println("DEBUG: Webcheck:" + inputLine);
					comanda = inputLine;
				}
			}
			in.close();
		}
		catch (Exception e)
		{
			System.out.println("Something went wrong:" + e);
		}
		return comanda;
	}
	
Edited by TroubleChick
  • 0
Posted

Ye it should. Try something, debug all lines, not only the first, to make sure that is not related with <br />, if the first check debugs you the line splitted in 2 lines it will be coz <br />, I guess.

  • 0
Posted (edited)

But the file has only 1 line. I'll try to make it html and at the end of line ill put a </br /> to see if its working.

 

LE: Same thing even if it's HTML extension.

 

Not even this way works:

			StringBuilder responseData = new StringBuilder();
			
			while ((inputLine = in.readLine()) != null)
			{
				responseData.append(inputLine);
			}
			System.out.println(responseData.toString());
			comanda = responseData.toString();
Edited by TroubleChick
  • 0
Posted (edited)

I can't help you at all coz I don't understand how do you test it.

Could you explain me what do you do, to be able to do the same as you and get the same problem? ;(

 

Edit: I'm just trying to schedule checks and works fine... it must be problem of your php thing... maybe the first check is trying to access to the file while php is changing it...

Edited by ^Wyatt
  • 0
Posted (edited)

Ok first of all. Im trying to make a script that read a line from a text file from a website.

 

1.If i add the script in gameserver and put to run in Gameserver.java first time it doesen't read the whole line and only few words from it. The next schedule it's reading the whole line.

 

2. I tried to make the script run by itself without putting in gameserver just compiling a java class and it works everytime. i see the output correctly and seeing the whole line.

 

LE: i edit the .txt file manual not with the php script and it works perfectly. Any idea what could php script have?

Edited by TroubleChick
  • 0
Posted

But when you have the script running from the gameserver, what about the .txt file and the php? Is the php chaning it?

Or when does the php act here?

  • 0
Posted

The php script is only to edit the .txt file as i showed you the script a little bit upper

The php act here to. Php is changing the .txt file.

 

 

In theory: I have a file .txt on a server and a .php. The php script is a textarea that edit the txt file. The java script from server reads what is in txt and do what's saying there. Problem is that is not reading the whole line!

  • 0
Posted (edited)

You don't get what am I asking. I wonder WHEN the php script acts and by who.

The php script must be acting when java is trying to check, otherwise it has no sense.

Edited by ^Wyatt
Guest
This topic is now closed to further replies.


×
×
  • Create New...