0

I'm trying to replace the occurence of a certain String from a given text file. Here's the code I've written:

BufferedReader tempFileReader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
File tempFileBuiltForUse = new File("C:\\testing\\anotherTempFile.txt");
Writer changer = new BufferedWriter(new FileWriter(tempFileBuiltForUse));
String lineContents ;
while( (lineContents = tempFileReader.readLine()) != null)
{
    Pattern pattern = Pattern.compile("/.");
    Matcher matcher = pattern.matcher(lineContents);
    String lineByLine = null;
    while(matcher.find())
    {
        lineByLine = lineContents.replaceAll(matcher.group(),System.getProperty("line.separator"));
        changer.write(lineByLine);
    }
}
changer.close();
tempFileReader.close();

Suppose the contents of my tempFile are:

This/DT is/VBZ a/DT sample/NN text/NN ./. 

I want the anotherTempFile to contain :

This/DT is/VBZ a/DT sample/NN text/NN . with a new line.

But I'm not getting the desired output. And I'm not able to see where I'm going wrong. :-( Kindly help. :-)

0

4 Answers 4

3

A dot means "every character" in regular expressions. Try to escape it:

Pattern pattern = Pattern.compile("\\./\\.");

(You need two backslahes, to escape the backslash itself inside the String, so that Java knows you want to have a backslash and not a special character as the newline character, e.g. \n

Sign up to request clarification or add additional context in comments.

2 Comments

Much better. But now, the output generated is every word in newline. Still not what I want.
Just a minor edit, for the pattern to be matched. Just for /. it'll be the same thing you suggested before?
2

In a regex, the dot (.) matches any character (except newlines), so it needs to be escaped if you want it to match a literal dot. Also, you appear to be missing the first dot in your regex since you want the pattern to match ./.:

Pattern pattern = Pattern.compile("\\./\\.");

Comments

2

Your regular expression has a problem. Also you don't have to use the Pattern and matcher. Simply use replaceAll() method of the String class for the replacement. It would be easier. Try the code below:

        tempFileReader = new BufferedReader(
                new InputStreamReader(new FileInputStream("c:\\test.txt")));
        File tempFileBuiltForUse = new File("C:\\anotherTempFile.txt");
        Writer changer = new BufferedWriter(new FileWriter(tempFileBuiltForUse));
        String lineContents;
        while ((lineContents = tempFileReader.readLine()) != null) {
            String lineByLine = lineContents.replaceAll("\\./\\.", System.getProperty("line.separator"));
            changer.write(lineByLine);
        }
        changer.close();
        tempFileReader.close();

Comments

1

/. is a regular expression \[any-symbol]. Change into to `/\\.'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.