File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java. util. regex. PatternSyntaxException

 
subash subash
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Alan Moore
Ranch Hand
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Alan! Glad to be corrected and learn something!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic