Java I/O Problem

I am trying to read from a textfile, a string containing also (from java's point of view) "complicated" characters such as "\" or "/". The content of the file is the following(without quotes):

"com.microsoft.sqlserver.jdbc.SQLServerDriver

jdbc:sqlserver://FIREBLADE\\SQLEXPRESS

"

I read it using:

BufferedReader input = new BufferedReader(new FileReader(filename));

driver = input.readLine();

url = input.readLine();

input.close();

If I try to print the Strings "driver" and "url" to System.out, both look the same as in the corresponding text-file, but if I compare them to "themselves", using

if (driver.compareTo("com.microsoft.sqlserver.jdbc.SQLServerDriver") != 0)

System.out.println("Wrong driver string");

if (url.compareTo("jdbc:sqlserver://FIREBLADE\\SQLEXPRESS") != 0)

System.out.println("Wrong url string");

it prints "Wrong url string" on the screen, which means that, although for System.out, both representations look the same, the url containing "/" and "\" is internally represented differently than the one which it is compared to. The "driver" matches in both representations.

I obviously cannot connect to the SQL Server, using this "url" read from the textfile, although it could connect to perfectly if the "url" was given statically.

My question is how can one read a string containing some non-standard characters from a file line-by-line, so as to be represented the same way in memory as they are seen inside the text-file.

Thank you for your attention!

[1588 byte] By [domnul_mihneaa] at [2007-11-27 8:56:01]
# 1

FIREBLADE\\SQLEXPRESS

These "\\" on your String is interpreted as only one ("\"), so type "\\\\"

And to compare 2 Strings you may use string.equals()

If it still don't work, try to use string.trim().equals()

sometimes you can get CRLF or null bytes , and "trim()" helps in that case

try this:

if (!driver.trim().equals("com.microsoft.sqlserver.jdbc.SQLServerDriver"))

System.out.println("Wrong driver string");

if (!driver.trim().equals("jdbc:sqlserver://FIREBLADE\\\\SQLEXPRESS"))

System.out.println("Wrong url string");

Return If you still having problems

cya

Message was edited by:

pbulgarelli

pbulgarellia at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for your quick reply!

Well, trim() does not help, and doubling "\" neither.

If I only leave the"/" special char in the string it is ok, therefore, the "\" character is interpreted wrong, but doubling it (inside the file only, inside the program only, in both) does not fix the problem.

domnul_mihneaa at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 3

Man, I can't reproduce the error.

But I hope you did not copied the code that I posted, cause they compare the "driver" twice.

In the second time you must compare url variable

anyway,

I created the file with the strings and all works fine when I use "\\\\" to represent "\\"...

If you didnt copied the code, tell me, cause I get interested now, hehe

cya

pbulgarellia at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 4

No, I didn't copy your code ad-literam, I understood the idea and tried to apply it. Also, I need the EXACT String "jdbc:sqlserver://FIREBLADE\\SQLEXPRESS", because this is the string expected by Microsoft SQL Server 2005. If I leave out the part "\\SQLEXPRESS" from my textfile and do something like

url = input.readLine();

url += "\\SQLEXPRESS";

it works just fine, but it removes the dynamicity from the connectivity, since connection strings may differ significantly, when using different JDBC drivers for different data base management systems. Also, this kind of string (any kind of string) must be read correctly regardless of its content, as long as the content is pure character(non-binary). Another connection string might contain many more "\" characters, spread inside it or other "uncomfortable" characters.

There must be some solution for "\", other than the doubling "\" attempt.

domnul_mihneaa at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 5
The file should only have one \. The code should have two, i.e. \\. javac processes escapes while BufferedReader doesn't.
ejpa at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 6

url += "\\SQLEXPRESS";

This should probably be:

url += "\\\\SQLEXPRESS";

All the evidence suggests to me that the string is being read correctly. It prints out correctly using System.println(), for example. The escape character "\" in the string literal seems to be the only problem here.

myncknma at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 7

First, when I say to put \\, instead \, is just when you are typing in the code. when you pass the String to connect you should not do this...

and

> If I leave out the part "\\SQLEXPRESS" from my

> textfile and do something like

>

> url = input.readLine();

> url += "\\SQLEXPRESS";

>

> it works just fine

So input.readLine() stops when he gets "\\" from your String?

System.out.println(url) must print "jdbc:sqlserver://FIREBLADE", rigth?

I really think your file is corrupted, or in another charset...

because input.readLine() must stop only when he gets a "\n" or eof.

what os are you running this code?

cya

pbulgarellia at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 8
Ignore all that, see reply #5.
ejpa at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 9
Ignore everything. Read up on escape sequences. http://www.developer.com/java/article.php/970481#1a http://sofia.fhda.edu/gallery/java/unit03/lesson03-2.html (Search for the first occurence of the word "escape")
myncknma at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...
# 10

Thanks to all of you for your replies and advices, and in particular to ejp, who provided me with the correct solution:

The file should only have one \. The code should have two, i.e. \\. javac processes escapes while BufferedReader doesn't.

domnul_mihneaa at 2007-7-12 21:18:13 > top of Java-index,Java Essentials,Java Programming...