aspose file tools*
The moose likes I/O and Streams and the fly likes java. util. regex. PatternSyntaxException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java. util. regex. PatternSyntaxException" Watch "java. util. regex. PatternSyntaxException" New topic
Author

java. util. regex. PatternSyntaxException

subash subash
Greenhorn

Joined: Jun 08, 2005
Posts: 2
i tired to read a file using

File inputFile = new File("templateFile.jnlp");
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String data = null;
if (!(bufferedReader.toString().matches("${fn}")))
{
System.out.println(data);
//write into the file
}
else
{
System.out.println("else...");

}


NOTE:
my actual need is to read a file and when i came across the "${fn}" string i
shoul replace it with some othere string. Then copy the modified thing into a new file.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Subash,
I can see that you have completely gone off the path. I will edit your code to what you need and thereafter explain it:



First of all, toString() method of any clas returns a description about the class or something of that sort. So, in this case toString() method should not be used.

Java provides funtionality to read one line at a time from a file. In the case of BufferedReader it is readLine(). It throws IOException.

So first you should read a line from the file and assign it to the data variable like this:



Then you should check the value of data. If it is null then the end of file has reached otherwise a single line has been returned. Now check whether this line contains the required String with the help of indexOf() method of String class.



If the value returned is -1 then the line does not contain the required String otherwise a positive value will be returned.

Then replace all the occurances of the required String within that line by simply calling replaceAll() method of String class.

To cycle through all the lines use a loop. Preferably a while loop.

NOTE: DON"T FORGET TO AGAIN ASSIGN ANOTHER LINE TO data variable usong readLine() method otherwise the loop will run infinitely.

Hope this helps you.


With Best Regards,
Shyam Prasad Murarka
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40



I don't think this will work. The replaceAll() method expects a regex as the first parameter. And I don't think "${fn}" is a valid regex.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Right, the problem is with the ${fn} string. dollar sign and curley braces are all special characters, so you need to escape them to use them. RegEx expects a single \ for escape, and you have to use two to get one in a Java String. So try "\\$\\{fn\\}" or Matcher.quoteReplacement("${fn}"); One of those oughtta work.

I'd skip checking indexof first. It might be a tiny bit more efficient than letting the replace method discover there are none, but make a profiler prove it's a problem before introducing more logic for humans to dig through when they read it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
You also need to assign the result of the replacement back to the variable:
Matcher.quoteReplacement(String) is for the second argument to replaceAll; it only escapes dollar signs and backslashes, because they're the only characters that are special in the replacement string. To escape the regex, use Pattern.quote(String).
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Thanks, Alan! Glad to be corrected and learn something!
 
 
subject: java. util. regex. PatternSyntaxException