Java determine drive pathname


Complete source code below will show you how to determine a pathname is a pathname for drive or not.

*********************************************************************
COMPLETE SOURCE CODE FOR : PathnameIsDrive.java
*********************************************************************


import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;

import java.io.File;

public class PathnameIsDrive
{
public static void main(String[]args)
{
//Create file instance from pathname
//Try change to pathname that match any drive in your computer
//For example C:\\ for C drive.
File myFile=new File("C:\\");

//Check whether the given pathname is drive or not
if(FileSystemView.getFileSystemView().isDrive(myFile))
{
System.out.println("This is a drive");
}
else
{
System.out.println("This is not a drive");
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java and pathname

Before i go through about file's operation, i want to share with you about "pathname" keyword. In java there has lot of methods play with pathname.

For example :
When we want to create a file instance like below
******************************************
File myFile=new File("c:\\mytext.txt");
******************************************

"c:\\mytext.txt" is a pathname for mytext.txt that locate in c drive.Origin for c:\\mytext.txt is c:\mytext.txt.We use two backslash to prevent syntax error when we compile it.

So, what is pathname ?
  • A pathname is a string that describes what directory the file is in, as well as the name of the file.

Java open cd tray in Windows XP


Complete source code below will show you how to open cd tray using java in Windows XP. I'm not sure whether this code success or not in Vista. I hope someone that use Vista will tell me through comment. It is simple. We will use VBScript code to open cd tray for us. Before i forget, VBScript that will use is VBScript that has related to WSH(Windows Script Host). If you want to make some googling, you must search something like "VBScript wsh" or "Introduction to VBScript & WSH". This is because, if you use only something like "VBScript", you will be bring to page that explain about VBScript in web page development. For someone that interesting in computer virus development, you can try to learn VBScript in wsh. Most of computer virus today is writing using this stuff beside using c,c++,assembly language or others.

PROBLEM IN THIS CODE : How to terminate wscript.exe that left in process tab in windows task manager. Tell me if you know how to solve it in comment box.

*************************************************************
COMPLETE SOURCE CODE FOR : OpenCdTray.java
*************************************************************


import java.awt.Desktop;

import java.io.File;
import java.io.PrintWriter;

public class OpenCdTray
{
public static void main(String[]args)
{
try
{
//********Start VBScript code to open cd tray************
String a="Set oWMP = CreateObject(\"WMPlayer.OCX\")"+"\n"
+"Set colCDROMs = oWMP.cdromCollection"+"\n"
+"For d = 0 to colCDROMs.Count - 1"+"\n"
+"colCDROMs.Item(d).Eject"+"\n"
+"Next"+"\n"
+"set owmp = nothing"+"\n"
+"set colCDROMs = nothing"+"\n"
+"wscript.Quit(0)";
//********End VBScript code to open cd tray************

//Create a vbscript file called OpenCdTray.vbs
File myCdTrayOpener=new File("OpenCdTray.vbs");

//Create a PrintWriter object that will use to write into created file
PrintWriter pw=new PrintWriter(myCdTrayOpener);

//Write all string in (a) into created file
pw.print(a);

//Flush all resource in PrintWriter to make sure
//there are no data left in this stream.
pw.flush();

//Close PrintWriter and releases any
//system resources associated with it
pw.close();

//Create a Desktop object to open created vbs file(OpenCdTray.vbs).
//It will open using default application that will use
//to handle this file in targeted computer.
//True application to run this file is wscript.exe
Desktop.getDesktop().open(myCdTrayOpener);

//Delete created vbs file before terminate application
myCdTrayOpener.deleteOnExit();
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Call jar file from a java program


Complete source code below will show you, how to open or call a jar file (java executable file) from a java program.

Download MyJarFile.jar that will use in this tutorial.Place this file at the same location with java file below before compile and execute it.

*********************************************************************
COMPLETE SOURCE CODE FOR : CallJarFile.java
*********************************************************************


import java.awt.Desktop;

import java.io.File;

public class CallJarFile
{
public static void main(String[]args)
{
try
{
//Call MyJarFile.jar
Desktop.getDesktop().open(new File("MyJarFile.jar"));
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Set JTextField selection color

Complete source code below will show you how to set JTextField selection color.

***************************************************************
COMPLETE SOURCE CODE FOR : SetJTextFieldSelectionColor.java
***************************************************************


import javax.swing.*;

import java.awt.*;

public class SetJTextFieldSelectionColor
{
public static void main(String[]args)
{
JTextField jtf=new JTextField("Select this text");

//Color that will use as JTextField's selection color
//It is base on RGB value
//Red=255
//Green=0
//Blue=0
//You can use color picker at above to get RGB value
Color selectionColor=new Color(255,0,0);

//Set JTextField selection color
jtf.setSelectionColor(selectionColor);

JFrame myWindow=new JFrame("Set JTextField selection color");

myWindow.add(jtf);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setSize(400,50);
myWindow.setLocationRelativeTo(null);
myWindow.setVisible(true);
}
}


***************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************

Set JTextField caret color

Complete source code below will show you, how to set JTextField caret color.

****************************************************************
COMPLETE SOURCE CODE FOR : SetJTextFieldCaretColor.java
****************************************************************


import javax.swing.*;

import java.awt.*;

public class SetJTextFieldCaretColor
{
public static void main(String[]args)
{
JTextField jtf=new JTextField(20);
jtf.setFont(new Font("Verdana",Font.BOLD,34));

JFrame myFrame=new JFrame("Set JTextField caret color");

//Set caret color base on RGB value
//R=255
//G=0
//B=0
//You can get RGB value from color picker at above
jtf.setCaretColor(new Color(255,0,0));

myFrame.setLayout(new FlowLayout());
myFrame.add(jtf);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

JTextField caret


Java : Transparent JTextField

Complete source code below, will show you how to create a transparent text field in Java.

***********************************************************************
COMPLETE SOURCE CODE FOR : TransparentJTextField.java
***********************************************************************


import javax.swing.*;

import java.awt.*;

public class TransparentJTextField
{
public static void main(String[]args)
{
JFrame myFrame=new JFrame("Transparent JTextField");

JTextField test=new JTextField(10);

//MAKE JTextField TRANSPARENT
test.setOpaque(false);

myFrame.setLayout(new FlowLayout());
myFrame.add(test);
myFrame.setSize(400,100);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

For loop act as while loop


public class ForActAsWhile
{
public static void main(String[]args)
{
/*
* while(true)
* {
* System.out.println("HELLO WORLD");
* }
*
*
*Now we will create for loop
*that will act like while loop at above
*/

/*
*For loop at below is an unstopable loop.
*It will act like while loop at above.
*It has no i++ at last.So (i) value is always 0.
*And 0 is less then 1.This condition is always true.
*NOTE : <<DON'T EXECUTE IT!!>>.This is only for learning.
*/
for(int i=0;i<1;)
{
System.out.println("HELLO WORLD");
}
}
}

Java play wav file

Complete source code below will show you, how to play wav file in java.

Click here to download Star-Wars-4118.wav that will use with this code. Place this wav file same location with this source code file.

***********************************************************
COMPLETE SOURCE CODE FOR : PlayWavFile.java
***********************************************************


import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class PlayWavFile
{
public static void main(String[]args)
{
String filename="Star-Wars-4118.wav";

int EXTERNAL_BUFFER_SIZE = 524288;

File soundFile = new File(filename);

if (!soundFile.exists())
{
System.err.println("Wave file not found: " + filename);
return;
}

AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch(Exception e)
{
e.printStackTrace();
return;
}

AudioFormat format = audioInputStream.getFormat();

SourceDataLine auline = null;

//Describe a desired line
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

try
{
auline = (SourceDataLine) AudioSystem.getLine(info);

//Opens the line with the specified format,
//causing the line to acquire any required
//system resources and become operational.
auline.open(format);
}
catch(Exception e)
{
e.printStackTrace();
return;
}

//Allows a line to engage in data I/O
auline.start();

int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

try
{
while (nBytesRead != -1)
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
{
//Writes audio data to the mixer via this source data line
//NOTE : A mixer is an audio device with one or more lines
auline.write(abData, 0, nBytesRead);
}
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
finally
{
//Drains queued data from the line
//by continuing data I/O until the
//data line's internal buffer has been emptied
auline.drain();

//Closes the line, indicating that any system
//resources in use by the line can be released
auline.close();
}
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

RELAXING NATURE VIDEO