JTester JTester - 1 month ago 11
Java Question

Unable to save readLine(); to the string variable

I'm trying to save readLine() output to the variable (it contains only one single line) to compare this lines in the future. But variables do not change their values:

try {
String VersName = "src/bin/version.txt";
BufferedReader br = null;
br = new BufferedReader(new FileReader(VersName));
URL url = new URL("ftp://31.130.206.175/current_versions/vers");

// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println(br.readLine());
System.out.println(in.readLine());

String aa = null;
String bb = null;
aa = br.readLine();
bb = in.readLine();
System.out.println("***" + aa + "***" + bb + "***");

in.close();
br.close();

} catch (MalformedURLException e3) {
System.out.println("Malformed URL");
} catch (IOException e4) {
System.out.println("I/O Error");
}


Output:

0.0.0
0.1.0
***null***null***


Is there any way to save them to variable?

Thank you.

Answer

Your problem is here

System.out.println(br.readLine());
System.out.println(in.readLine());

You read your line, so it is no longer available for your

aa = br.readLine();
bb = in.readLine();

And so you read nothing.

Try to reorder your instructionslike this (for example):

String aa = br.readLine();
String bb = in.readLine();

System.out.println(aa);
System.out.println(bb);

It should be better ;)