Java
Convert unicode codepoints to unicode hex values in java
In a part of our crawler development, we encountered a Bangla news site (http://www.kalerkantho.com/) which uses code points instead of unicode hex valus in their website. Although it renders banla fonts in browser, but when viewings source it only shows code points, so when downloaded by crawler we only got কકى similar. For the indexing purpose we needed to convert them to hex values so that it renders bangla font anywhere. The process of converting is really so simple.
1 CommentExtract hyperlinks from html using regular expression in java
2 years ago, I worked with an crawler which can fetch webpages from internet, then parse the links from the page and then visit all the pages linked to the page. At that time I didn't have any idea about regular expressions. So I had to write around a 500 hundred line code to parse links and meta tags from html.
Yesterday, I had to do the same job again. This time I took up regular expression to parse the html <a tags followed by href attribute to extract the links.
Regular expressions can be difficult to understand if written at once, so I am going to write it in easy way first, then i ll make it complex to support variations in page links.
1 CommentCreate a lan messenger using socket programming in java
For my networking course lab project I developed an messenger for lan using java. It uses socket protocol for communication.
1 CommentGenerate random number within a range in java
If want to generate random numbers within a minimum and maximum limit you can easily do this in java using this function.
public int getRandom(int min, int max)
{
return (int) (Math.random() * (max - min + 1) ) + min;
}
No CommentsCreate a notepad like windows in java
When I was in 3rd semester I had a project to create a notepad in java. I tried to make it look closer to windows notepad as much as possible. It includes 5 java classes.
The main thing is ofcourse the notepad.java class First take a look at the code
4 CommentsReading an web page source using java
I needed to develop an web crawler few days ago. My first challange was to read a website source code using java. Here is the code I wrote to read a webpage. It will read the http response of url given and print it out.
package webcrawler;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/** * * @author Burhan */
public class readUrl
{
StringBuffer content;
String contentLowerCase;
String line;
String type = null;
URL url = null;
URLConnection urlConnection;
InputStream urlStream;
DataInputStream html;
public readUrl(String urlStr) throws MalformedURLException, IOException
{
url = new URL(urlStr);
urlConnection = url.openConnection();
urlStream = url.openStream();
type = urlConnection.getContentType();
if(type==null)
return;
else if( type.compareTo("text/html") != 0 ) // not allowed type
return; // only htmls are here
content = new StringBuffer();
html = new DataInputStream(urlStream);
while ((line = html.readLine()) != null)
{
content.append(new StringBuffer(line));
content.append('\n');
}
System.out.print(content);
}
public static void main(String args[])
{
try
{
new readUrl("http://www.dscripts.net");
}
catch(MalformedURLException ex)
{
ex.printStackTrace();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
No CommentsHow to lock a process in java to prevent multiple instance at the same time
If you create a java program and run the jar file more than twice. You will see that same program is running two instance at the same time. There may be some events when you really dont want to see multiple instances of same program. You can prevent this in this easy way. The theory is to create a file using java and lock the file when program runs. If another instance is tried to execute it will try to access the same file. But it wont be able to that because the file is already openned and locked by another program (i.e another instance). Thus we will be able to exit the second instance showing a message that the program is already running.
Here the lock class that implements this feature.
1 CommentCompress and uncompress a java byte array using deflater and enflater
In java a byte[] array is similar as a binary data stored in a file. So you can compress and uncompress this array content using deflater and enflater class in java. This is done completely in memory so this is very much faster than zipping or unzipping a file using java.
In this example I will show you how to compress a string by converting it to byte and then compressing storing it in compressed byte array it takes smaller memory. And when needed just extract the byte array to its full size and convert to string again.
1 CommentCompress file to gz format using java
Java programs can compress a file on a fly! All you need is just to make proper use of java.util.zip class like this
public class gZip
{
public static void compress(String src, String dest) throws java.io.IOException
{
java.util.zip.GZIPOutputStream out = new java.util.zip.GZIPOutputStream(new java.io.FileOutputStream(dest));
java.io.FileInputStream in = new java.io.FileInputStream(src);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
// Complete the GZIP file
out.finish();
out.close();
}
public static void main(String[] args)
{
try
{
compress("D://test.txt", "D://test.txt.gz");
}
catch (java.io.IOException ex)
{
ex.printStackTrace();
}
}
}
This will compress D:/test.txt to D:/test.txt.gz
No Comments