On
Java
Topics:
1. Inet address
2. Factory method
3. URL connection
4. File handling
Submitted to:
submitted by:
Mr. Prakash
Ankit Kumar
089
MCA(4th
sem)
1. InetAddress
The InetAddress class has no visible constructors. To create an
InetAddress object, you have to use one of the available factory
methods. Factory methods are merely a convention whereby
static methods in a class return an instance of that class. This is
done in lieu of overloading a constructor with various parameter
lists when having unique method names makes the results much
clearer. In the case of InetAddress, the three methods
getLocalHost(), getByName(), and getAllByName() can be used to
create instances of InetAddress. These methods are shown here:
throws UnknownHostException
throws UnknownHostException
throws UnknownHostException
The getLocalHost( ) method simply returns the InetAddress object that
represents the local host. The getByName( ) method returns an
InetAddress for a host name passed to it. If these methods are unable
to resolve the host name, they throw an UnknownHostException.On
the Internet, it is common for a single name to be used to represent
several machines. In the world of web servers, this is one way to
provide some degree of scaling. The getAllByName( ) factory method
returns an array of InetAddresses that represent all of the addresses
that a particular name resolves to. It will also throw an
UnknownHostException if it can't resolve the name to at least one
address.
import java.net.*;
class InetAddressTest
UnknownHostException {
System.out.println(Address);
Address = InetAddress.getByName("starwave.com");
System.out.println(Address);
System.out.println(SW[i]);
}
Here is the output produced by this program. (Of course, the output
you see will be slightly different.)
default/206.148.209.138
starwave.com/204.202.129.90
Uses of Class
java.net.InetAddressPackages that use InetAddress
programming language.
2 Factory method
Provides an abstraction or an interface and lets subclass or
implementing classe decide which class or method should be
instantiated or called, based on the conditions or parameters given.
Benefits:
• Connect parallel class hierarchies.
reduces subclasses.
Examples
To illustrate such concept, let's use a simple example. To
paint a picture, you may need several steps. A shape is an
interface. Several implementing classes may be designed in
the following way.
interface Shape {
public void draw();
}
class Line implements Shape {
Point x, y;
Line(Point a, Point b) {
x = a;
y = b;
}
public void draw() {
//draw a line;
}
}
class Square implements Shape {
Point start;
int width, height;
Square(Point s, int w, int h) {
start = s;
width = w;
height = h;
}
public void draw() {
//draw a square;
}
}
class Circle implements Shape {
....
}
class Painting {
Point x, y;
int width, height, radius;
Painting(Point a, Point b, int w, int h, int r) {
x = a;
y = b;
width = w;
height = h;
radius = r;
}
Shape drawLine() {
return new Line(x,y);
}
Shape drawSquare() {
return new Square(x, width, height);
}
Shape drawCircle() {
return new Circle(x, radius);
}
....
}
...
Shape pic;
Painting pt;
//initializing pt
....
if (line)
pic = pt.drawLine();
if (square)
pic = pt.drawSquare();
if (circle)
pic = pt.drawCircle();
3. Uniform Resource Locator (URL)
A Uniform Resource Locator (URL) specifies the location of a resource
on the Web. The HTMLReaderBean class shows how to connect to a
URL from within an enterprise bean.
java.net.URLConnection
Context context;
URL url;
StringBuffer buffer;
String line;
int responseCode;
HttpURLConnection connection;
InputStream input;
BufferedReader dataInput;
try {
url = (URL)context.lookup("java:comp/env/url/MyURL");
connection = (HttpURLConnection)url.openConnection();
responseCode = connection.getResponseCode();
}
if (responseCode != HttpURLConnection.HTTP_OK) {
String.valueOf(responseCode));
try {
input = connection.getInputStream();
dataInput =
buffer.append(line);
buffer.append('\n');
return buffer;
}
4. File handling:
File handling is an integral part of nearly all programming
projects. Files provide the means by which a program stores data,
accesses stored data, or shares data. As a result, there are very
few applications that don’t interact with a file in one form or
another. Although no aspect of file handling is particularly
difficult, a great many classes, interfaces, and methods are
involved. Being able to effectively apply them to your projects is
the mark of a professional.
Filename handling
However if you define a path in your file name then you have to
take care the path separator. On windows system the '\' is used
and you need to backslash it so you need to write '\\', in
Unix,Linux systems
To open a file for writing use the FileWriter class and create an
instance from it.
This code opens the file in overwrite mode. If you want to append to
the file then
a try-catch block.
Write to file
At this point we have a writer object and we can send real content to
the file. You can do this using the write() method, which has more
variant but the most commonly used requires a string as input
parameter.
Calling the write() method doesn't mean that it immediately writes the
data into the file. The output is maybe cached so if you want to send
your data immediately to the file you need to call the flush() method.
As last step you should close the file with the close() method and you
are done.
3. try {
5. writer.write("Test text.");
6. writer.close();
7. }
8. catch (IOException e) {
9. e.printStackTrace();
10. }
11. }