In this post, i am going to describe how to generate JTree in swing according to an XML document dynamically.
Used XML File : catalog.xml
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Java : Html form parser return map of (name,value) pair of input attribute
Simple HTML Form Parser which parses the given string of HTML and returns Map of (name,value) pair of form's input attribute.
CODE : MyHtmlFormParser class
Java : Counting frequency of word in a string using Map
Use a Map to map Map<String,Integer> the words with frequency.
Then you can find out for a particular word with:
String SPACE =" ";
String [] words = input.split(SPACE);
Map<String,Integer> frequency = new HashMap<String,Integer>();
for (String word:words){
Integer f = frequency.get(word);
frequency.put(word,f+1);
}
Then you can find out for a particular word with:
frequency.get(word);
Java: ping an URL from java application - code example
The code to ping a URL from java application is as follows :
public static void pingUrl(final String address) {
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
urlConn.connect();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ping to "+address +" was success");
}
} catch (final IOException e) {
e.printStackTrace();
}
}
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
urlConn.connect();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ping to "+address +" was success");
}
} catch (final IOException e) {
e.printStackTrace();
}
}
With response time:
public static void pingUrlWithResponseTime(final String address) {
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
final long startTime = System.currentTimeMillis();
urlConn.connect();
final long endTime = System.currentTimeMillis();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Time (ms) : " + (endTime - startTime));
System.out.println("Ping to "+address +" was success");
}
} catch (final IOException e) {
e.printStackTrace();
}
}
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
final long startTime = System.currentTimeMillis();
urlConn.connect();
final long endTime = System.currentTimeMillis();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Time (ms) : " + (endTime - startTime));
System.out.println("Ping to "+address +" was success");
}
} catch (final IOException e) {
e.printStackTrace();
}
}
java find path of current executing jar
Best solution :
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
You can use your own class name instead of 'Test'
Xsl transform in Java working example (xml to html)
In order to display XML documents, it is necessary to have a mechanism to describe how the document should be displayed. One of these mechanisms is Cascading Style Sheets (CSS), but XSL (eXtensibleStylesheet Language) is the preferred style sheet language of XML.
XSL can be used to define how an XML file should be displayed by transforming the XML file into a format such as HTML, PDF, etc..
XSL can be used to define how an XML file should be displayed by transforming the XML file into a format such as HTML, PDF, etc..
Java - Single instance of application - working source code example
Having a single instance of an application is crucial in most of the software. In this article, I am giving an example on how to implement single instance of an Application.
How it works :
How it works :
- New instance of application tries to connect to a specific ServerSocket (localhost, port#) to detect running applications. And a running application must have a ServerThread to detect possible run of new instance of the same application
- The main Logic in steps
- Find existing server socket running on localhost
- If found(another instance was already running) --> exit current instance of application
- else --
- start a new Server thread to detect run of future applications
- and start the application
Silence Removal and End Point Detection JAVA Code
For the purpose of silence removal of captured sound, we used the algorithm in our final year project. In this post, I am publishing the endpoint detection and silence removal code ( implementation of this algorithm in JAVA).
These links might be useful to you as well.
These links might be useful to you as well.
- For Capturing audio from microphone
- For Converting captured data into PCM integer or float array
- For Playing any PCM array of amplitudes
- array of original signal's amplitude data : float[] originalSignal
- sampling rate of original signal in Hz : int samplingRate
package org.ioe.tprsa.audio.preProcessings;
BlueChess : Two Player Chess Game in J2ME, My minor project @ IOE
Here is the download link for BlueChess : Two Player Chess Game
http://www.4shared.com/file/JWiI8Cce/BlueChess_Bluetooth_two_player.html
http://www.4shared.com/file/JWiI8Cce/BlueChess_Bluetooth_two_player.html
XML parsing using SaxParser with complete code
SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure. You should extend DefaultHandler and override few methods to achieve xml parsing.
The methods to override are
The following example demonstrates the uses of DefaultHandler to parse and XML document. It performs mapping of xml to model class and generate list of objects.
The methods to override are
- startDocument() and endDocument() – Method called at the start and end of an XML document.
- startElement() and endElement() – Method called at the start and end of a document element.
- characters() – Method called with the text contents in between the start and end tags of an XML document element.
The following example demonstrates the uses of DefaultHandler to parse and XML document. It performs mapping of xml to model class and generate list of objects.
Java-Open/Launching any file with its default handler with start utility in windows
Java-Open/Launching any file with its default handler with start utility in windows
String cmd = "cmd.exe /c start ";
String file = "c:\\version.txt";
Runtime.getRuntime().exec(cmd + file);
Mouse Gesture Recognition with Hidden Markov Model.
Understanding gestures can be posed as a pattern recognition problem. These patterns(gestures) are variable but distinct and have an associated meaning. Since gesture consists of continuous motion in sequential time, an HMM is an effective recognition tool.
My work is a demonstration of a mouse motion gesture recognition system using Dynamic HMM. It is developed in Java.
Code Available @ : https://github.com/gtiwari333/mouse-gesture-recognition-java-hidden-markov-model
Main References were :
DEMO VIDEO
My work is a demonstration of a mouse motion gesture recognition system using Dynamic HMM. It is developed in Java.
Code Available @ : https://github.com/gtiwari333/mouse-gesture-recognition-java-hidden-markov-model
Main References were :
DEMO VIDEO
Redirecting JAVA IO to GUI by Redirecting IO Streams
Here is a small utility program for fun (but useful). It is written in Java by redirecting IO Streams to GUI.
Its features are:
Its features are:
- Reading console input from GUI --> No need of Black Screen.
- Displaying console output (including exceptions) to GUI.
- Easy to use.
- Useful for Java Beginners
java zip- create text file from list of string and zip them
java - create text files from list of string and zip them ( without creating temporary file)
Testing the code :
private static void list_of_string_into_text_file_and_zip_them_all(List dataList) throws Exception {
File zipFile = new File("OutputZipFile.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
int count = 0;
for (String string : dataList) {
// repeat for each string
String fileNameInsideZip = ++count + "-inside_text_file.txt";
ZipEntry e = new ZipEntry(fileNameInsideZip);
out.putNextEntry(e);
// write data into zip entry
byte[] data = string.getBytes();
out.write(data, 0, data.length);
out.closeEntry();
}
out.flush();
out.close();
}
Testing the code :
public static void main(String[] args) throws Exception {
// store string into text file - and zip them . doesnot create a
// temporary text file.
List test = new ArrayList();
test.add("This is amazing");
test.add("What a wonderful world");
test.add("This is a beautiful day ");
list_of_string_into_text_file_and_zip_them_all(test);
}
For creating a single text file from string and zip it - see this post.
java zip - create text file of String and zip it
Create a text file with some string (without temporary file) and zip it : working source code java
Call as :
For creating text files from List of string and zip them - see my earlier post
private static void zip_single() throws Exception {
final StringBuilder sb = new StringBuilder();
sb.append("Test String that goes into text file inside zip");
final File f = new File("single_text_file_inside.zip");
final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
ZipEntry e = new ZipEntry("inside_text_file.txt");
out.putNextEntry(e);
byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
}
Call as :
public static void main(String[] args) throws Exception {
zip_single();
}
For creating text files from List of string and zip them - see my earlier post
Java open url in default system browser
open a url in system default browser - work for all : windows, mac, linux operating systems
Full working JAVA source code example
Full working JAVA source code example
import java.io.IOException;
public class StartBrowserUtil {
private StartBrowserUtil() {
}
public static void openURL(String s) {
String s1 = System.getProperty("os.name").toLowerCase();
Runtime runtime = Runtime.getRuntime();
try {
Java Image Processing : Negative of Input Image - source code
The code below is for getting negative of an input image in JAVA:
public class TestImagesss {
public static void main(String[] args) {
BufferedImage org = getImage("test.jpg");
BufferedImage negative = getNegativeImage(org);
new ImageFrame(org, "Original");
new ImageFrame(negative, "After Negative");
java hex -int-string-byte conversion source code
Hexadecimal conversion - string, byte, integer - working java source code example :
public final class HexConvertUtils {
private HexConvertUtils() {
}
public static byte[] toBytesFromHex(String hex) {
Java delete a folder and subfolders recursively
Java source code for deleting a folder and subfolders recursively.
Code for copying and moving files is here.
Working source code example:
Code for copying and moving files is here.
Working source code example:
public final class FileIOUtility {
private FileIOUtility() {}
public static boolean deleteFile(File fileToDelete) {
if (fileToDelete == null || !fileToDelete.exists()) {
return true;
} else {
Java - contro lkeyboard 'lock key' state programmatically
Java Controlling the lock keys : Caps Lock, Scroll Lock, Num Lock 's state - programmatically
public class Lock_Key_Control_In_Java {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
//enable disable key state Caps Lock, Scroll Lock, Num Lock keys
tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK, true);
tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false);
tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, false);
}
}
The lock keys LED in the keyboard should be turned ON or OFF ?
Subscribe to:
Posts
(
Atom
)