Java Code Snippets

Hi all,

I’ve been a great fan of Java and I thought of sharing some useful code snippets.

1. Given below is the Java code to open a web page.


import java.io.*;
public class mani {
public static void main(String args[])throws IOException{
try
{
Process p=Runtime.getRuntime().exec("cmd /c start www.google.com");
}
catch(IOException e1) {System.out.println(e1);}
}
}

Here, start followed by the web page, in this case http://www.google.com does the trick. You can open up a web page using this command from the Command/DOS prompt.

2. Here is the Java code to delete the contents of a text file, without using any File processing methods.


import java.io.*;
public class mani {
public static void main(String args[])throws IOException{
try
{
Process p=Runtime.getRuntime().exec("cmd /c type nul>c:\a.txt");
}
catch(IOException e1) {System.out.println(e1);}
}
}

With this code, you can delete the contents of a .txt file with a Java program. Here, type nul>filename does the trick.

I’ll be updating this post as and when I get any new ideas or sample code snippets from the Internet. Have fun guys. 🙂

Advertisements

About Manikandan Surendren

A PeopleSoft Techno-functional Consultant. An engineering graduate in Computer Science. Technology Enthusiast. Movie freak. Gadget geek.
This entry was posted in Code. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s