Tuesday, February 14, 2012

Getting IP address in java

try
{
InetAddress[] addresses = InetAddress.getAllByName( "http://wwwroboticsforall.blogspot.com/" /* Ip assress or Domain name */ );


for ( int i=0; i String hostname = addresses[i].getHostName();
               System.out.println(addresses[i].getHostAddress());
               System.out.println( hostname );

}
}


catch (UnknownHostException e)
{
System.out.println("unknown host");
}

Thursday, February 9, 2012

How to read file attibutes

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
*
* @author Saman
*/
//http://wwwroboticsforall.blogspot.com/


//Use following source file to test and lean java file attributes.


public class Attrib {


void print(String fileName)throws IOException,FileNotFoundException{
//Prints attributes in console.
//http://wwwroboticsforall.blogspot.com/

File file=new File(fileName);
System.out.println("\n"+file.getAbsoluteFile()+" file attributes..\n");
System.out.println(file.canExecute()?"Excutable\n":"Not a Excutable\n");
System.out.println(file.canRead()?"Readable\n":"Unreadable\n");
System.out.println(file.exists()?"File already exits\n":"File not Found\n");
System.out.println(file.canWrite()?"Writable\n":"Unwritable\n");
System.out.println(file.isDirectory()?"Directory\n":"Not a derectory\n");
System.out.println(file.isFile()?"File\n":"Not a file\n");
System.out.println(file.isHidden()?"Hidden File\n":"Visible File\n");


}

public static void main(String[] args) {
//http://wwwroboticsforall.blogspot.com/
try {

//Replace fileNameWithPath to created file on your computer.

//C:\boot.inf is a system files in windows operating systems and boot informations included

String fileNameWithPath="c:/boot.ini";
Attrib attrib=new Attrib();
attrib.print(fileNameWithPath);

} catch (Exception e) {
e.printStackTrace();
//if an error found prints in the console, so you can find out error easily.
}
}

}