![]() Programming Center Supplement Welcome to the Java Developer ConnectionSM New to Java Programming Center Supplement! This monthly supplement provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to the JDC's New to JavaTM Programming Center. CONTENTS
1. Java Programming Language Basics Exceptions and How to Handle Them
2. Making Sense of the Java Class Libraries
4. Java Bits Java 2 Platform, Standard Edition (J2SE) versus Java 2 Platform, Enterprise Edition(J2EE) What's the difference?
5. New to the New to Java Programming Center Building an Application, Part 2 Links to articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here.
7. Program Challenge Solution and Explanation
Possible solution to the
Java Programming Language BasicsExceptions and How to Handle Them
If you look at the list of keywords for the Java programming
language, you might think someone was into sports with
keywords like Defining the Terminology:
These are the basics of exception handling, provided you put the keywords together properly. And putting them together properly is what the rest of this article is about.
Exception handling is done with a
When multiple catch clauses are present, control transfers to the first exception type that matches the thrown exception.
For instance, if your exception types are
Here is an example using specific exception types, which is the framework for reading from a file.
There is one small problem with the above code.
If an exception is thrown while reading from the file,
the
Overcoming this problem requires the use of the
Notice that the
Until you learn the libraries, the compiler will tell
you when you aren't catching an exception. Then you
just go back and put the appropriate code block
within a Making Sense of the Java Class Libraries
|
import java.io.*;
public class FileReaderWriterExample
{
public static void main(String[] args)
throws IOException
{
// Create input stream object.
FileInputStream fis =
new FileInputStream("Data");
// Create output stream object.
FileOutputStream fout =
new FileOutputStream("Data2");
// Set variable for looping through bytes.
int c;
// Loop to read and write bytes.
while ((c = fis.read()) != -1)
fout.write(c);
// Close output and input resources.
fis.close();
fout.close();
}
}
|
This example reads and writes to files, but it does not account for the errors that frequently arise when dealing with files, such as non-existing files, files that are write-protected, and so forth.
To deal with possible errors, enclose the code
in try-catch blocks, and throw exceptions where needed,
as described in the previous section.
In addition to the FileInputStream and FileOutputStream classes,
the java.io package has many other classes you can use for
reading and writing to files. Read the documentation for the
I/O package, and experiment, using different classes.
Also, study the hierarchy of the stream classes. There are many useful inherited methods available, depending which classes you choose to use in your applications.
See For More Information below.
CopyFile
Use what you've learned about exception handling and reading
and writing to files to create a CopyFile Program Challenge:
try-catch blocks. There are many Java technologies, and understanding the differences between the various platforms, such as J2SE and J2EE, can be confusing. Which do you need, how do they overlap, do you need both? The answer to these questions lies in what kind of project you're working on.
The J2SE platform includes the compiler and other tools, the JVM, and the core classes. Core classes provide programmers with the capability to create simple and complex applets and applications in which they may include the following:
java.io package.java.sql packageIn addition, J2SE provides classes that make it possible to do networking, create beans, work with dates and mathematical functions, provide additional security, use collections, and much more.
The J2EE platform works with J2SE, extending the these technologies by providing the following services:
java.sql packageTo set up the J2EE platform, you must have J2SE installed. J2EE defines the standard for developing multitier enterprise applications, and ensures portability of applications across the wide range of existing enterprise systems capable of supporting J2EE.
On the other hand, you can use some of the above technologies without installing the J2EE platform.
For instance, you can make use of Java servlets by installing J2SE and a servlet engine like Tomcat. In addition, Tomcat also processes JavaServer pages.
For standalone applications, you likely only need:
To create web applications, using servlets or JSP Pages, you need to install:
To build enterprise applications that require use of EJBs, servlets, JSP pages, XML, and database connectivity, you need:
In addition to these two Java platforms, there are other Java technologies. You can download additional Java platforms such as Java 2 Platform, Micro Edition (J2ME) for wireless functionality, or optional software such as Java Web Start, which makes it possible to launch applications from a web page.
In summary, J2SE provides a lot of functionality to applets and applications, while J2EE extends the platform to include enterprise web services, deployment tools, and more.
See For More Information.
Part 2 of this tutorial series introduces inheritance, image and text objects, and arranging those objects using a layout manager.
Building an Application, Part 1 now also available in
Lesson: I/O: Reading and Writing (but no 'rithmetic)
Java 2 Platform, Standard Edition Product Family
J2SE, version 1.4 Summary of New Features and Enhancements
JavaServer Pages Tomcat @ Jakarta
New to Java Programming Center Building an Application Introduction
This is one possible solution to the Program Challenge:
import java.io.*;
public class CopyFile {
public static void main(String args[]) {
// Check for two command line arguments
if (args.length != 2) {
System.out.println(
"Improper arguments use:" +
" java CopyFile infile outfile");
System.exit(-1);
}
// Get infile argument
String infile = args[0];
// Get outfile argument
String outfile = args[1];
// Create variables for in/out streams
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// Create in stream
fis = new FileInputStream(infile);
// Create out stream
fos = new FileOutputStream(outfile);
// Declare variable for each byte read
int ch;
// Read byte til end of file
while ((ch = fis.read()) != -1) {
// Put bytes read into out stream
fos.write(ch);
}
// Catch FileNotFoundException
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e);
// Catch IOException
} catch (IOException e) {
System.err.println("I/O problems: " + e);
// Close files
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ignored) {
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException ignored) {
}
}
}
}
}
|
For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the Java 2, Standard Edition (J2SE) development kit.
IMPORTANT: Please read our Terms of Use and Privacy policies:
Have a question about programming? Use Java Online Support.
- Note -
Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems purposes only. You have received this email because you elected to subscribe.
- Unsubscribe -
To unsubscribe to this newsletter, go to the subscriptions page uncheck the "JDC New-to-Java Supplement" checkbox, and click "Update".
- Subscribe -
To subscribe to other JDC mailings, go to the subscriptions page choose the newsletters you want to subscribe, and click "Update".
- Feedback -
Comments? Send your feedback on the JDC Newsletter to: jdc-webmaster
- Copyright -
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA
This document is protected by copyright.
JDC New-to-Java Programming Center Supplement
December 2001
Sun, Sun Microsystems, Java, and J2SE are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.
|
| ||||||||||||