• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java. util. regex. PatternSyntaxException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Alan! Glad to be corrected and learn something!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic