WillieS1212
WillieS1212

Reputation: 33

How to replace multiple HTML titles in a directory using Java?

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());
    }

Upvotes: 2

Views: 676

Answers (1)

Sergio Flores
Sergio Flores

Reputation: 5427

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.

Upvotes: 1

Related Questions