2

I am creating a Java Application to replace or a title to an HTML file. I have a folder of HTML files that I want to change the titles for and I don't want to manually do every HTML file. I want to read the HTML, find the title tag (i.e. <title>Some Title Here</title>), and replace it with a new title (i.e <title>Some New Title Here</title>) in each HTML file in the folder.

Can this be done using java? And how?

Keep getting IO Exceptions

for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        File file = new File(listOfFiles[i].getName());
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line = in.readLine();
        while (line != null) {
            if (line.contains(type)) {
                line = line.replaceAll(
                        "(<title>[A-Za-z0-9\\s]+</title>)",
                        "<title>New title</title>");
            }

            lines.add(line);
            line = in.readLine();
        }
        in.close();

        PrintWriter out = new PrintWriter(listOfFiles[i]);
        for (String l : lines)
            out.println(l);
        out.close();

        System.out.println("File " + listOfFiles[i].getName());
    } else if (listOfF`enter code here`iles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
    }
1

1 Answer 1

1

You can read the content of each file and use a regular expression to replace the title like this:

package replacetitle;

import java.io.*;
import java.util.*;

public class ReplaceTitle {
    public static void main(String[] args) {
        try {
            File file = new File("c:\\demo.html");
            replaceSelected(file, "<title>");
        } catch(IOException ex) {
            System.out.println(ex.toString());
        }
    }

    public static void replaceSelected(File file, String type) throws IOException {
        // we need to store all the lines
        List<String> lines = new ArrayList<String>();

        // first, read the file and store the changes
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line = in.readLine();
        while (line != null) {
            if(line.contains(type)) {
                line = line.replaceAll("(<title>[A-Za-z0-9\\s]+</title>)","<title>New title</title>");
            }

            lines.add(line);
            line = in.readLine();
        }
        in.close();

        // now, write the file again with the changes
        PrintWriter out = new PrintWriter(file);
        for (String l : lines)
            out.println(l);
        out.close();
    }
}

And then save the file.

Hope it help.

Based on this question.

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

11 Comments

Yeah, isn't going to parse an entire [X]HTML, but helps to replace just the "<title>Anything</title>" that is what he needs.
What would happen if the <title> is written in 2 or more lines? How this RegEx will handle it?
I know, but generally a title tag appears just once in an HTML file, but that's not the real problem.
The real problem is to even advice to use regex for HTML parsing. It is plain wrong. It is not the right solution, and it won't be ever.
|

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.