How it began?
When I started a PHPBB Forum I defined BBCodes for Ads (Google Ads etc) in the board so that in any posts when I insert those BBCodes, corresponding Ads would be coming up.
The Problem:
Then I started an imagehost service which would give me a bunch of codes for the images that I would upload. I would normally be posting one image per post followed by an Ad. But sometimes the number would be so huge, I would just like to turn of the javascripts (so that I dont generate false page impressions on the advertisements), and then manually write the BBCodes after the image host codes (because javascript is disabled, clicking on the BB Codes wouldn’t insert the code.)
To make it more clear, my image host codes would be something like:
[url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url]
and I had to manually type the bbcodes on the next line following the URL (because I disabled javascripts for posting).
[url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url]
[gad3][/gad3]
With a huge number of images, it is sometimes cumbersome to type in those BB Codes myself.
This java utility that I wrote basically reads a bunch of those URL Lines from a file, appends a random BBCode (I have three ads that I would like to show, chosen at random) after each line and then save it to back to the file. After that all I have to do is copy-paste the URL+BBcode and post it.
/** * @Author Kushal Paudyal * www.sanjaal.com/java * Last Modified On: 23rd July 2009 */ package com.kushal.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class BBCodeAppender { static String[] bbCodes = { "gad1", "gad2", "gad3" }; static String modifiedText = ""; public static void main(String args[]) { String fileName = "C:/temp/bbcodeappendtest.txt"; appendBBCodesOnLines(fileName); } public static void appendBBCodesOnLines(String fileName) { try { /** * Read the file and append the BB Code */ BufferedReader in = new BufferedReader(new FileReader(fileName)); String line; while ((line = in.readLine()) != null) { if (line.trim().length() > 0) //to avoid appending to the empty lines { line += "\n" + getRandomBBCode() + "\n"; modifiedText += line; } } in.close(); /** * Write the modified text back to the file. */ FileWriter fstream = new FileWriter(fileName); BufferedWriter out = new BufferedWriter(fstream); out.write(modifiedText); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static String getRandomBBCode() { String bbCode = ""; int index = (int) (Math.random() * 100) % bbCodes.length; bbCode = "[" + bbCodes[index] + "][/" + bbCodes[index] + "]"; return bbCode; } }
==========
Input File Content:
[url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url]
Output File Content:
[url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad3][/gad3] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad1][/gad1] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad2][/gad2] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad1][/gad1] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad3][/gad3] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad2][/gad2] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad2][/gad2] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad3][/gad3] [url=http://sanjaal.com/ihost/share-EDC8.html][img]http://sanjaal.com/ihost/image-EDC8.jpg[/img][/url] [gad2][/gad2]
Originally posted 2009-07-23 13:28:44.